浙江大学acm 1904 老是wrong answer? 程序我自己本地测试没问题!
#include <stack>
#include <stdio.h>
#include <iostream>
using namespace std;
struct myMatrix
{
int line;
int row;
};
int main()
{
int num;
stack<char> stb;
stack<myMatrix> stm;
myMatrix *mat;
char express[50];
while(cin>>num)
{
mat=new myMatrix[num];
char matName;
for(int i=0;i<num;i++)
{
cin>>matName;
cin>>mat[char(matName)-'A'].line>>mat[char(matName)-'A'].row;
}
char ex;
int result=0;
int p;
while(cin>>express)
{
p=0;
while(1)
{
ex=express[p++];
if(ex=='(')
stb.push(ex);
else
if(ex==')')
{
myMatrix a,b;
int r;
b=stm.top();
stm.pop();
a=stm.top();
stm.pop();
stb.pop();
if(a.row==b.line)
{
r=a.line*a.row*b.row;
a.row=b.row;
}
else
r=-1;
stm.push(a);
if(r==-1)
result=-1;
else
result=result+r;
}
else
stm.push(mat[ex-'A']);
if(stb.empty())
break;
}
stm.pop();
if(result==-1)
cout<<"error\n";
else
cout<<result<<endl;
result=0;
}
delete []mat;
}
return 0;
}
测试地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=94
谢谢啦!
[解决办法]
不会是一行字符数超过50吧?不会是运算次数超过int范围吧?
会不会是while(cin>>num)这行,没必要while?看题目似乎就第一行是num
[解决办法]
第2行修改应是:
else if (result!=-1) //改 else