ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

2740๋ฒˆ: ํ–‰๋ ฌ ๊ณฑ์…ˆ

์ฒซ์งธ ์ค„์— ํ–‰๋ ฌ A์˜ ํฌ๊ธฐ N ๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ํ–‰๋ ฌ A์˜ ์›์†Œ M๊ฐœ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” ํ–‰๋ ฌ B์˜ ํฌ๊ธฐ M๊ณผ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ M๊ฐœ์˜ ์ค„์— ํ–‰๋ ฌ B์˜ ์›์†Œ K๊ฐœ

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/2740
// ํ–‰๋ ฌ์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์ „์— ๋จผ์ € ํ’€์–ด์•ผ ํ•  ๋ฌธ์ œ
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m, k int
	fmt.Fscanln(reader, &n, &m)

	var a = make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscanf(reader, "%d ", &a[i][j])
		}
	}

	fmt.Fscanln(reader, &m, &k)
	var b = make([][]int, m)
	for i := 0; i < m; i++ {
		b[i] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Fscanf(reader, "%d ", &b[i][j])
		}
	}
	var result = make([][]int, n)
	for i := 0; i < n; i++ {
		result[i] = make([]int, k)
		for j := 0; j < k; j++ {
			for l := 0; l < m; l++ {
				result[i][j] += a[i][l] * b[l][j]
			}
		}
	}

	for i := 0; i < n; i++ {
		for j := 0; j < k; j++ {
			fmt.Fprintf(writer, "%d ", result[i][j])
		}
		fmt.Fprintln(writer, "")
	}
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/2740
# ํ–‰๋ ฌ์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์ „์— ๋จผ์ € ํ’€์–ด์•ผ ํ•  ๋ฌธ์ œ
import sys

if __name__ == "__main__":
    n, m = list(map(int, sys.stdin.readline().split()))
    a = []
    for i in range(n):
        a.append(list(map(int, sys.stdin.readline().split())))

    m, k = list(map(int, sys.stdin.readline().split()))
    b = []
    for i in range(m):
        b.append(list(map(int, sys.stdin.readline().split())))

    result = [[0 for i in range(k)] for j in range(n)]
    for i in range(n):
        for j in range(k):
            for l in range(m):
                result[i][j] += a[i][l] * b[l][j]
    
    for i in range(n):
        for j in range(k):
            print("{} ".format(result[i][j]), end='')
        print()
728x90
๋Œ“๊ธ€