如果A是个x行y列的矩阵,B是个y行z列的矩阵,把A和B相乘,其结果将是另一个x行z列的矩阵C。这个矩阵的每个元素是由下面的公式决定的
矩阵的大小不超过100*100
第一行包含一个正整数x,代表第一个矩阵的行数
第二行包含一个正整数y,代表第一个矩阵的列数和第二个矩阵的行数
第三行包含一个正整数z,代表第二个矩阵的列数
之后x行,每行y个整数,代表第一个矩阵的值
之后y行,每行z个整数,代表第二个矩阵的值
对于每组输入数据,输出x行,每行z个整数,代表两个矩阵相乘的结果
2 3 2 1 2 3 3 2 1 1 2 2 1 3 3
14 13 10 11
1 2 3 3 2 1 乘以 1 2 2 1 3 3 等于 14 13 10 11
16 8 7 17 19 16 19 14 1 14 9 7 2 7 9 16 14 16 12 13 3 3 17 5 9 8 16 1 14 16 10 13 13 14 1 13 13 15 4 7 2 6 16 16 15 5 5 15 13 1 11 11 5 0 16 14 7 7 15 0 16 4 7 16 6 0 15 2 14 11 2 17 17 5 12 8 13 11 10 1 17 10 8 15 16 17 15 7 8 13 14 5 19 11 3 11 14 5 4 9 16 13 11 15 18 0 3 15 3 19 9 5 14 12 3 9 8 7 11 18 19 14 18 12 19 9 1 0 18 17 10 5 18 16 19 6 12 5 1 17 1 5 9 16 3 14 16 4 0 19 3 6 11 9 15 18 11 17 13 5 5 19 3 16 1 12 12 13 19 1 10 5 18 19 18 6 18 19 12 3 15 11 6 5 10 17 19
1020 1490 1063 1100 1376 1219 884 966 1035 1015 715 1112 772 920 822 948 888 816 831 920 863 855 1099 828 578 1160 717 724 745 1076 644 595 930 838 688 635 1051 970 600 880 811 846 748 879 952 772 864 872 878 526 722 645 335 763 688 748 764 996 868 362 1026 681 897 836 1125 785 637 940 849 775 1082 1476 996 968 1301 1183 953 609 987 717 401 894 657 662 700 1083 1022 527 1016 746 875 909 1162 905 722 1055 708 720 1126 1296 1240 824 1304 1031 1196 905 1342 766 715 1028 956 749
while True: try: a=[]#记录矩阵A b=[]#记录矩阵B x = int(input())#记录x y = int(input())#记录y z = int(input())#记录z for i in range (x): #连续读入矩阵A数据 str1 = input().split() str1 = list(map(int,str1))#元素转换为int 用于计算 a.append(str1)#添加进矩阵A以列表形式 for i in range (y):#连续读入矩阵B数据 str2 = input().split() str2 = list(map(int,str2))#元素转换为int 用于计算 b.append(str2)#添加进矩阵B以列表形式 for i in range (x):#矩阵A逐行,共x行,每处理完全部列换下一行(横着) strout = ""#初始化打印 for ii in range (z): #矩阵B逐列从第一列开始共z列(竖着),处理完一列竖着全部元素换下一列 result=0#初始乘的值 for j in range (y):#全部元素对应行列(横竖)相乘,a[0][j]则是第一行的第j个 乘以第 b[j][0] 一列的第j个,之后列数增加完后行数增加继续直到结束 result =result+ a[i][j] * b[j][ii] strout = strout + str(result)+ " " print(strout) except: break
while True: try: x = int(input()) y = int(input()) z = int(input()) A = [] B = [] C = [[0 for i in range(z)] for j in range(x)] for i in range(x): A.append([int(j) for j in input().strip().split()]) for i in range(y): B.append([int(j) for j in input().strip().split()]) for i in range(x): for j in range(z): for k in range(y): C[i][j] += A[i][k]*B[k][j] for i in C: print(' '.join(map(str, i))) except: break
while True: try: x=int(input()) y=int(input()) z=int(input()) list1=[0]*x list2=[0]*y list3=[[0 for j in range(z)] for k in range(x)] for i in range(x): list1[i]=list(map(int,input().split())) for j in range(y): list2[j]=list(map(int,input().split())) for i in range(x): for j in range(z): w=0 for u in range(y): w+=list1[i][u]*list2[u][j] list3[i][j]=w for i in range(x): print(' '.join(list(map(str, list3[i])))) except: break
while True: try: x = int(input()) y = int(input()) z = int(input()) A = [] B = [] C = [[] for _ in range(x)] for i in range(x): A.append(list(map(int, input().split()))) for i in range(y): B.append(list(map(int, input().split()))) for i in range(x): for j in range(z): C_ij = 0 for k in range(y): C_ij += A[i][k]*B[k][j] C[i].append(C_ij) print(*C[i]) except: break
def sum_a1b1(a1, b1): sum1 = 0 for a, b in zip(a1, b1): sum1 += a * b return sum1 def col2row(bb): rows = len(bb) cols = len(bb[0]) b2 = [] for col in range(cols): b = [] for row in range(rows): b.append(bb[row][col]) b2.append(b) return b2 def run(): while True: try: x = int(input().strip()) y = int(input().strip()) z = int(input().strip()) a_in = [] b_in = [] for row in range(x): a_in.append(list(map(int, input().strip().split()))) for row in range(y): b_in.append(list(map(int, input().strip().split()))) res = [] bb = col2row(b_in) for a in a_in: tmp = [] for b in bb: tmp.append(sum_a1b1(a, b)) res.append(tmp) for row in res: print(*row) except: break run()
while True: try: x, y, z = int(input()), int(input()), int(input()) m1, m2 = [], [] for i in range(x): m1.append(list(map(int, input().split()))) for j in range(y): m2.append(list(map(int, input().split()))) m = [[0]*z for _ in range(x)] for i in range(x): for j in range(z): n = 0 for k in range(y): n += m1[i][k]*m2[k][j] m[i][j] = n for i in range(x): print(" ".join(list(map(str, m[i])))) except: break
while True: try: x, y, z = map(int,(input(),input(),input())) a, b = [], [] c = [[0 for i in range(z)] for j in range(x)] for i in range(x): a.append(list(map(int,input().split()))) for i in range(y): b.append(list(map(int,input().split()))) for i in range(x): for j in range(z): for k in range(y): c[i][j] += a[i][k] * b[k][j] for i in range(x): print(*c[i]) except: break
while True: try: h1, l1_h2, l2 = int(input()), int(input()), int(input()) # 两个矩阵相乘 前一个的列 和 后一个的行 要一样,这也是题目只给了一个数字的原因 arr1 = [list(map(int, input().strip().split())) for i in range(h1)] arr2 = [list(map(int, input().strip().split())) for i in range(l1_h2)] # 为了方便我们写程序,我们对 arr2 作一下转置 arr2 = [list(i) for i in zip(*arr2)] res = [] for a1 in arr1: row = [] for a2 in arr2: row.append(sum(map(lambda x:x[0]*x[1], zip(a1, a2)))) res.append(row) [print(' '.join(map(str, i))) for i in res] except: break
while True: try: x = int(input()) y = int(input()) z = int(input()) a = [] b = [] for i in range(x): a.append(list(int(input().split(" ")))) for i in range(y): b.append(list(int(input().split(" ")))) for k in range(x): res = [] for i in range(z): ans = 0 for j in range(y): temp = b[j][i]*a[k][j] ans = temp + ans res.append(ans) print(" ".join(map(str,res))) except: break这样为什么不过啊?
while True: try: x = int(input()) y = int(input()) z = int(input()) a = [] b = [] for i in range(x): line = list(map(int, input().split())) a.append(line) for i in range(y): line = list(map(int, input().split())) b.append(line) c = [[0 for _ in range(z)] for _ in range(x)] for i in range(x): for j in range(z): c[i][j] = sum([a[i][temp]*b[temp][j] for temp in range(y)]) for i_a in range(x): for i_b in range(z): if i_b == (z-1): print(c[i_a][i_b]) else: print(c[i_a][i_b], end=' ') except EOFError: pass
while True: try: x, y, z = int(input()), int(input()), int(input()) one = [list(map(int, input().split())) for i in range(x)] two = [list(map(int, input().split())) for j in range(y)] new = [[] for q in range(z)] for h, o in enumerate(new): for g in two: o.append(g[h]) out = [[] for l in range(x)] for m, value1 in enumerate(one): for p, value2 in enumerate(new): unit = 0 for n, value3 in enumerate(value1): unit += value3 * value2[n] out[m].append(unit) for a in out: print(' '.join(map(str, a))) except: break
def rotate(ls): a = len(ls) b = len(ls[0]) lst = [] for i in range(b): ls_t = [] for j in range(a): ls_t.append(ls[j][i]) lst.append(ls_t) return lst def chenghe(ls1,ls2): a = len(ls1) sum = 0 for i in range(a): sum += ls1[i]*ls2[i] return sum def chengfa(ls1,ls2): ls3 = rotate(ls2) a = len(ls1) b = len(ls3) matrix = [[0 for j in range(b)]for i in range(a)] for i in range(a): for j in range(b): matrix[i][j] = str(chenghe(ls1[i],ls3[j])) return matrix def printf(matrix): for k in matrix: print(' '.join(k)) while True: try: a = eval(input()) b = eval(input()) c = eval(input()) ls1,ls2 = [],[] for i in range(a): ls1.append(list(map(eval,input().split()))) for j in range(b): ls2.append(list(map(eval,input().split()))) matrix = chengfa(ls1,ls2) printf(matrix) except: break
while 1: try: m,n,p = int(input()), int(input()), int(input()) mat1, mat2, prod = [], [], [] for i in range(m): prod.append([]) for i in range(m): mat1.append(list(map(int,input().split()))) for i in range(n): mat2.append(list(map(int,input().split()))) for i in range(m): for j in range(p): temp = 0 m1, m2 = mat1[i], list(a[j] for a in mat2) for k in range(len(m1)): temp += m1[k]*m2[k] prod[i].append(temp) for i in prod: out = ' '.join(list(map(str,i))) print(out) except: breaklist当数组用确实比numpy差远了。。。
while True: try: x = int(input()) y = int(input()) z = int(input()) A = [[0 for j in range(y)] for i in range(x)] B = [[0 for j in range(z)] for i in range(y)] C = [[0 for j in range(z)] for i in range(x)] for i in range(x): line = input().split() A[i] = line for i in range(y): line = input().split() B[i] = line for i in range(x): for k in range(z): row = A[i] col = [line[k] for line in B] C[i][k] = sum(list(map(lambda x: int(x[0])*int(x[1]), zip(row,col)))) for i in range(x): for j in range(z): print(C[i][j], end=' ') print() except: break