Problem J: 小火山的计算能力
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 433 Solved: 103
Submit Status Web Board
Description
别人说小火山的计算能力不行,小火山很生气,于是他想证明自己,现在有一个表达式,他想计算出来。
Input
首先是一个t(1<=20)表示测试组数。然后一个表达式,表达式长度不超过200,只有加法和减法,并且保证第一个字符不会是运算符号,最终结果小于2^63-1。
Output
输出运算结果。
Sample Input
21+12+1-1
Sample Output
22
HINT
对字符串处理的有趣的题目,考验基本功及一定的思维能力
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
const int N = 3000;
char c[N],cmp[22];
LL a[105],amp[22];
int main() {
int T;
scanf("%d",&T);
while(T--) {
memset(a,0,sizeof(a));
scanf("%s",c);
int flag=0,temp=0;
LL sum=0;
char cl='+';
for(int l=0; l<=strlen(c); l++) {
if(c[l]!='+'&&c[l]!='-'&&c[l]!='\0') {
cmp[temp]=c[l];
temp++;
// printf("%d--=\n",temp);
} else {
for(int i=0; i<temp; i++) {
a[flag]=a[flag]*10+(cmp[i]-'0');
// printf("%d--\n",a[flag]);
}
if(cl=='-') a[flag]=-a[flag];
flag++;
cl=c[l];
temp=0;
}
}
for(int l=0; l<flag; l++) sum+=a[l];
printf("%lld\n",sum);
}
return 0;
}
题目链接:http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1158&pid=9