题解 | #矩阵乘法#

矩阵乘法

https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b?tpId=37&tqId=21292&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

import sys


'''
矩阵A和矩阵B的信息的初始化
'''
Aline = int(input())
Arow = int(input())

Bline = Arow

Brow  = int(input())

A = []
B = []

'''
将AB 矩阵的数据添加到列表中
'''
for i in range(Aline):
    row = list(map(int,input().split(" ")))
    A.append(row)


for i in range(Bline):
    row = list(map(int,input().split(" ")))
    B.append(row)




def  MatrixMultiplication(Aline,Arow,Bline,Brow,A,B):
  '''
  矩阵的运算结果先遍历A的行,再遍历B的列,再设置一个变量k,K的大小为矩阵A的列数或者B的行数
  '''
  
   #初始化一个矩阵C,用来存放运算结果
    C=[[0]*Brow for i in range(Aline)]
    
    for i in range(Aline):
        for j in range(Brow):
            for k in range(Bline):
                temp = A[i][k] * B[k][j]
                C[i][j] += temp	
    #print(C)
    for i in range(Aline):
        for j in range(Brow):
            print(C[i][j],end=" ")
		#输出完一行以后,打印一个空行,换行输出
        print() 

    







MatrixMultiplication(Aline,Arow,Bline,Brow,A,B)

全部评论

相关推荐

shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务