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

矩阵乘法计算量估算

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

#include<stdio.h>
#include<string.h>
#include<ctype.h>

typedef struct Node{
    int rowNum;
    int colNum;
}Matrix;

int main()
{
    int n;
    while(EOF != scanf("%d",&n))
    {
        Matrix matrix[n];
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&matrix[i].rowNum,&matrix[i].colNum);
        }
        char str[100];
        scanf("%s",str);
        Matrix stack[n];
        int index = -1;
        
        int alphaidnex = -1;
        int len = strlen(str);
        
        int countSum = 0;
        
        for(int i=0;i<len;i++)
        {
            if(isupper(str[i]))
            {
                ++alphaidnex;
                ++index;
                stack[index].rowNum = matrix[alphaidnex].rowNum;  // 入栈
                stack[index].colNum = matrix[alphaidnex].colNum;
            }else if(str[i] == ')')
            {
                Matrix temp;  
                temp.rowNum = stack[index-1].rowNum;
                temp.colNum = stack[index].colNum;
                countSum += stack[index-1].rowNum*stack[index].colNum*stack[index-1].colNum;
                index -= 2;   //出栈计算
                ++index; // 入栈计算结果
                stack[index].rowNum = temp.rowNum;
                stack[index].colNum = temp.colNum;
            }
        }
        printf("%d\n",countSum);
    }
    
    return 0;
}
全部评论

相关推荐

码农索隆:7*24,随时待命,这是去🇷🇺打仗去了啊
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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