Python向量形式存储来处理矩阵相乘
原生设计思路:
矩阵相乘说到底就是行vector*列vector,在处理输入矩阵的时候就按照行vector和列vector存储,最后只需要两个for就可以。
时间复杂度O(n^2)
m, k, n = int(input()), int(input()), int(input()) mat1 = [] for _ in range(m): mat1.append(list(map(int, input().split()))) mat2 = [[] for _ in range(n)] for _ in range(k): nums = list(map(int, input().split())) for j in range(n): mat2[j].append(nums[j]) for i in range(m): row = '' for j in range(n): temp = 0 for a in range(k): temp += mat1[i][a]*mat2[j][a] row+=str(temp)+' ' print(row.strip())