生成一个8行6列的矩阵A,其元素值等于该元素所在位置的行号+列号; 再生成一个6行12列的矩阵B,其元素值是小于或等于20的随机数; 计算前两步生成的矩阵A和矩阵B的乘积,输出计算结果。并且不用到numpy库
时间: 2024-05-09 09:21:06 浏览: 103
```python
import random
# 生成矩阵A
A = []
for i in range(8):
row = []
for j in range(6):
row.append(i*j)
A.append(row)
# 生成矩阵B
B = []
for i in range(6):
row = []
for j in range(12):
row.append(random.randint(1, 20))
B.append(row)
# 计算A和B的乘积
C = []
for i in range(len(A)):
row = []
for j in range(len(B[0])):
temp = 0
for k in range(len(B)):
temp += A[i][k] * B[k][j]
row.append(temp)
C.append(row)
# 输出结果
for row in C:
for element in row:
print(element, end='\t')
print()
```
阅读全文