蓝桥杯BASIC-17矩阵乘法——python

蓝桥杯BASIC-17矩阵乘法——python,第1张

蓝桥杯BASIC-17矩阵乘法——python
m,n=map(int,input().split())    #m阶矩阵 幂为n
if n==0:
    l = [[0] * m for i in range(m)]
    for i in range(m):
        l[i][i]=1
    for i in range(m):
        for j in range(m):
            print(l[i][j],end=" ")
        print()

else:
    l1=[]
    l2=[]
    a=0
    for i in range(m):
        l1.append(input().split())
    l=[[0]*m for i in range(m)]
    l3=[[0]*m for i in range(m)]
    for i in range(m):
        for j in range(m):
            l[i][j]=int(l1[i][j])
            l3[i][j] = int(l1[i][j])
    for i in range(n-1):
        for p in range(m):
            for q in range(m):
                for o in range(m):
                    a=a+l[p][o]*l3[o][q]
                l2.append(a)
                a=0
        t=0
        for e in range(m):
            for r in range(m):
                l[e][r] = l2[t]
                t+=1
        l2=[]

    for i in range(m):
        for j in range(m):
            print(l[i][j],end=" ")
        print()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5072945.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存