题解 | #矩阵乘法#

矩阵乘法

https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b

import sys

x, y, z = int(input().strip()), int(input().strip()), int(input().strip())

ma = [list(map(int, input().strip().split())) for i in range(x)]

mb = [list(map(int, input().strip().split())) for i in range(y)]

def matrix_multiply(la, lb):
    res = 0
    for i in range(len(la)):
        res += la[i] * lb[i]
    return res
    
def col_list(matrix, col):
    res = []
    for i in matrix:
        res.append(i[col])
    return res

mbc = [col_list(mb, i) for i in range(z)]

res = [[0 for j in range(z)] for i in range(x)]

for i in range(x):
    for j in range(z):
        res[i][j] = matrix_multiply(ma[i], mbc[j])

for i in range(len(res)):
    print(*res[i], sep=' ')

全部评论

相关推荐

北冥有鱼吗:工作忙,现在没工作了哈哈哈
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务