P1981(NOIP2013 普及组)表达式求值题解
Step—1 命名(不再讲述)
int num,s=0,t;
char c;
Step—2 读入+判断
因为只有乘号和加号,所以…
直
接
干
!
cin>>t;
while(scanf("%c",&c) && c!='\n')
{
cin>>num;
if(c=='*')
t=t*num%10000;
else
s=(s+t)%10000,t=num;
}
注意:
scanf
记得加&
;- 先输入一个t,再开干;
Step—3 输出答案
cout<<(s+t)%10000;
Code
#include<bits/stdc++.h>
using namespace std;
int num,s=0,t;
char c;
int main()
{
cin>>t;
while(scanf("%c",&c) && c!='\n')
{
cin>>num;
if(c=='*')
t=t*num%10000;
else s=(s+t)%10000,t=num;
}
cout<<(s+t)%10000;
return 0;
}