题解- 数组 | #矩阵乘法#

矩阵乘法

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

while True:
    try:
        x = int(input())
        y = int(input())
        z = int(input())
        x_y = [[0 for i in range(y)] for j in range(x)]
        y_z = [[0 for i in range(z)] for j in range(y)]
        result = [[0 for i in range(z)] for j in range(x)]
        for i in range(x):
            set_y = list(map(int,input().split()))
            for j in range(y):
                x_y[i][j]=set_y[j]
        for i in range(y):
            set_z = list(map(int, input().split()))
            for j in range(z):
                y_z[i][j]=set_z[j]
                
        for i in range(x):
            for j in range(z):
                sum = 0
                for k in range(y):
                    sum+=x_y[i][k]*y_z[k][j]
                result[i][j]=sum
            print(" ".join(list(map(str,result[i]))))
    except:
        break

好像无法插入numpy数据库,只能用循环一点点算了。一些常见的操作可以背下来,写代码会更快点:

  1. 将一次性输入的数据转化为数组:
set_x = list(map(int, input().split()))
  1. 将数组打印出来
print(" ".join(list(map(str,set_x))))
  1. 在python中用列表生成式来表示矩阵
[[0 for i in range(n)] for j in range(m)]

生成一个m*n 的矩阵

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务