题解 | #矩阵乘法#
矩阵乘法
http://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
# print([[0 for _ in range(3)] for _ in range(4)])
#[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
while True:
try:
x = int(input())
y = int(input())
z = int(input())
A = []
B = []
for i in range(x):
A.append(list(map(int,input().split())))
for j in range(y):
B.append(list(map(int,input().split())))
#输入录入后,开始计算,先初始化一个二维数组,初始值为0
R=[[0 for k in range(z)] for i in range(x)]
for i in range(x):
for k in range(z):
for j in range(y): #计算每个输出单元格的数据,A行与B列的乘积,长度为y
R[i][k] += A[i][j] * B[j][k]
#按行输出
for i in range(x):
for k in range(z):
print(R[i][k], end = ' ')
print('')# 因为上面的print(end=' ')必须在' '后面再print一个值,后面的print才能换行
except:
break
#[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
while True:
try:
x = int(input())
y = int(input())
z = int(input())
A = []
B = []
for i in range(x):
A.append(list(map(int,input().split())))
for j in range(y):
B.append(list(map(int,input().split())))
#输入录入后,开始计算,先初始化一个二维数组,初始值为0
R=[[0 for k in range(z)] for i in range(x)]
for i in range(x):
for k in range(z):
for j in range(y): #计算每个输出单元格的数据,A行与B列的乘积,长度为y
R[i][k] += A[i][j] * B[j][k]
#按行输出
for i in range(x):
for k in range(z):
print(R[i][k], end = ' ')
print('')# 因为上面的print(end=' ')必须在' '后面再print一个值,后面的print才能换行
except:
break
【牛客站内】华为机试题—中等 文章被收录于专栏
【牛客站内】华为机试题练习记录