JZOJ 1768 expression evaluates expr
Description–
Given a containing only addition and multiplication arithmetic expression, the value of your program to evaluate the expression.
Input–
Input file expr.in .
Input only one row, your expression is required for the calculation, the expression contains only numbers, the addition operator "+" and multiplication
method the operator "*", and without brackets, all numbers involved in computing are 0 to 231 - an integer between 1.
Input data to ensure that this line only 0 to 9, +, * These 12 characters.
Output–
Output file name expr.out.
Output only one line containing an integer that represents the value of the expression.
Note: When the answer to the length of more than 4, only the last four output request, leading 0 is not output.
Sample Input–
expr.in1
1+1*3+4
expr.in2
1 + 1234567890 * 1
expr.in3
1+1000000003*1
Sample Output–
expr.out1
8
expr.out2
7891
expr.out3
4
Notes -
【data range】
For 30% of the data, 0≤ expression addition operator and the multiplication operator the total number of ≦ 100;
For 80% of data, 0≤ expression addition operator and the multiplication operator Total ≤1000;
For 100% of the data, 0≤ expression Total ≤100000 addition operator and the multiplication operator.
Problem-solving ideas -
For each number appears taken only after its 4
Code -
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int t,fy,a[100005];
char b[100005];
string s;
int main()
{
//freopen("expr.in","r",stdin);
//freopen("expr.out","w",stdout);
cin>>s;
for (int i=0;i<s.size();++i)
if (s[i]<='9' && s[i]>='0')
fy=fy*10+int(s[i]-48);
else
a[++t]=fy%10000,fy=0,b[t]=s[i];
a[++t]=fy%10000;
for (int i=1;i<=t;++i)
if (b[i]=='*')
a[i+1]=(a[i+1]*a[i])%10000,a[i]=0;//乘法
for (int i=2;i<=t;++i)
a[1]=(a[1]+a[i])%10000;//加法
printf("%d",a[1]);
return 0;
}