题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

n = int(input())  # 矩阵的个数
arr = []        # 存储矩阵
order = []  # 存储计算法则
res = 0
#输入矩阵的行列信息
for i in range(n):
    arr.append(list(map(int,input().split())))

# 输入计算法则
f = input()

# 遍历运算法则
for i in f:     # f=(A(BC))
    if i.isalpha():
        order.append(arr[ord(i)-65])
    elif i == ')' and (len(order) >= 2):
        a = order.pop() # 取出矩阵C=[20,5]
        b = order.pop() # 取出矩阵B=[10,20]
        res += b[0]*a[1]*a[0]       # 累计
        order.append([b[0],a[1]])      # [10,5]

print(res)

全部评论
均是参考别人的,共同学习
点赞
送花
回复
分享
发布于 01-31 16:51 广东

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务