1913: 小火山的计算能力
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 443 Solved: 106
Submit Status Web Board
Description
别人说小火山的计算能力不行,小火山很生气,于是他想证明自己,现在有一个表达式,他想计算出来。
Input
首先是一个t(1<=20)表示测试组数。然后一个表达式,表达式长度不超过200,只有加法和减法,并且保证第一个字符不会是运算符号,最终结果小于2^63-1。
Output
输出运算结果。
Sample Input
2 1+1 2+1-1
Sample Output
2 2
HINT
Source
wa了15就是因为ZZULIOJ不支持'\n'................:-(,内心是崩溃的
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int t,i;
long long a,b,sum;
char c,str[220];
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%s",&str);
int len=strlen(str);
i=0;
a=0;
while(i<len)
{
if(str[i]>='0'&&str[i]<='9')
{
a=a*10+(str[i]-'0');
i++;
}
else
break;
}
if(i>=len)
{
printf("%lld\n",a);
continue;
}
c=str[i];
i++;
sum=a;
b=0;
while(i<len)
{
while(str[i]>='0'&&str[i]<='9')
{
b=b*10+str[i]-'0';
i++;
}
if(c=='+')
sum+=b;
else
sum-=b;
c=str[i];
i++;
b=0;
}
printf("%lld\n",sum);
}
return 0;
}