UVA442矩阵乘法之求出多个矩阵相乘乘法的次数

63 篇文章 0 订阅
52 篇文章 0 订阅

这个题自己刚开始写的,受前面那个题的影响开了俩个栈,一个拿来存括号了,然而这个题保证输入合法,

不需要将括号入栈,另外自己刚开始还重重载了矩阵赋值函数,后来发现矩阵竟然不需要重载赋值函数,不知道为什么矩阵不需要

重载赋值函数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cctype>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
#include<algorithm>
#define LL long long
using namespace std;
struct matrix
{
    int r,c;
    matrix(int r,int c):r(r),c(c){}
    matrix() {}
   /* matrix operator = (matrix b)
    {
        this->r=b.r;
        this->c=b.c;
        return *this;
    }*/
};
int main()
{
    int n;
    cin>>n;
   // getchar();
    matrix  ma[30];
    for(int i=0;i<n;i++)
    {
        char c;
        int a,b;
        cin>>c>>a>>b;
        int cc=c-'A';
        ma[cc].r=a;
        ma[cc].c=b;
    }
    getchar();
    string s;
    while(getline(cin,s))
    {
       int len=s.length();
       if(len==1)
       {
           cout<<0<<endl;
           continue;
       }
   //    stack<char> s1;
       stack<matrix> s2;
       int sum=0;
       int flag=1;
       for(int i=0;i<len;i++)
       {
          // if(s[i]=='(') s1.push(s[i]);
           if(isalpha(s[i])) s2.push(ma[s[i]-'A']);
           else if(s[i]==')')
           {
              // s1.pop();
               matrix a,b;
              // cout<<s2.top().r<<endl;
               a=s2.top();
               s2.pop();
               b=s2.top();
               s2.pop();
            //   cout<<b.r<<' '<<b.c<<' '<<a.r<<' '<<a.c<<endl;
               if(b.c!=a.r)
               {
                   flag=0;
                   break;
               }
               sum=sum+a.r*b.r*a.c;
               s2.push(matrix(b.r,a.c));
           }
       }
       if(flag)
        cout<<sum<<endl;
       else
        cout<<"error"<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值