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

while True:
    try:
        n = int(input())
        arr = []  # 装填输入的矩阵
        order = []  # 处理出入栈的矩阵
        res = 0  # 乘法次数
        for i in range(n):
            arr.append(list(map(int,input().split())))
        f = input()
        count = 0
        for i in f:
            if i.isalpha():
                order.append(arr[count])  # 将字符串的字母依序转化成arr对应的矩阵
                count += 1
            elif i == ')' and len(order) >=2:  # 碰到‘)’并且此时栈内有两个以上矩阵,开始计算
                b = order.pop()
                a = order.pop()
                res += a[1]*b[1]*a[0]  # 累计每次乘法次数
                order.append([a[0],b[1]])  # 将计算后得到的矩阵装进栈内
        print(res)
    except:
        break

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务