A = [[1,3,5],[7,9,11],[13,15,17]]
B = [[9,8,7],[6,5,4],[3,2,1]]
N = 3
C = [[None] * N for row in range(N)]
for i in range(3):
for j in range(3):
C[i][j] = A[i][j] + B[i][j]
print("矩阵A和矩阵B相加的结果:")
for i in range(3):
for j in range(3):
print("%d" %C[i][j],end="\t")
print()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2.3运行结果
def MatrixMultiply(arrA, arrB,arrC,M,N,P):
global C
if M<=0 or N<=0 or P<=0:
print('[错误:维数M,N,P必须大于0]')
return
for i in range(M):
for j in range(P):
Temp=0
for k in range(N):
Temp = Temp + int(arrA[i*N+k])*int(arrB[k*P+j])
arrC[i*P+j] = Temp
def main():
findword=0
namedata=['Allen','Scott','Marry','John','Mark','Ricky','Lisa','Jasica','Hanson','Amy','Bob','Jack']
data=[[1001,32367],[1002,24388],[1003,27556],[1007,31299], [1012,42660],[1014,25676],[1018,44145],[1043,52182], [1031,32769],[1037,21100],[1041,32196],[1046,25776]]
print('员工编号 薪水 员工编号 薪水 员工编号 薪水 员工编号 薪水')
print('-------------------------------------------------------')
for i in range(3):
for j in range(4):
print('%2d [%3d] ' %(data[j*3+i][0],data[j*3+i][1]),end='')
print()
head=employee() #建立链表头部
if not head:
print('Error!! 内存分配失败!!')
sys.exit(0)
head.num=data[0][0]
head.name=namedata[0]
head.salary=data[0][1]
head.next=None
ptr=head
for i in range(1,12): #建立链表
newnode=employee()
newnode.num=data[i][0]
newnode.name=namedata[i]
newnode.salary=data[i][1]
newnode.num=data[i][0]
newnode.next=None
ptr.next=newnode
ptr=ptr.next
while(True):
findword=int(input('请输入要删除的员工编号,要结束删除过程,请输入-1:'))
if(findword==-1): #循环中断条件
break
else:
ptr=head
find=0
while ptr!=None:
if ptr.num==findword:
ptr=del_ptr(head,ptr)
find=find+1
head=ptr
ptr=ptr.next
if find==0:
print('######没有找到######')