Main » 2013 October 6 » [模拟赛NOIP2010]加括号问题
8:53 AM [模拟赛NOIP2010]加括号问题 |
贪心(1A)#include<cstdio> #include<cstdlib> #include<cstring> #include<set> #include<algorithm> #include<map> #include<vector> #include<queue> #include<iostream> #include<string> #include<cmath> #define FOR(i,a,b) for(i=(a);i<=(b);i++) #define ROF(i,a,b) for(i=(a);i>=(b);i--) typedef long long LL; using namespace std; string S,st; int strtoint(string s) { int j=s.size()-1; int sum=0,x=1; while (j>=0) { int xn=s[j]-'0'; sum+=x*xn; x*=10; j--; } return sum; } int main() { ios::sync_with_stdio(false); cin>>S; int i=0,j; char ch='+'; int ans=0; while (i<S.size()) { if (ch=='+') { j=i; st=""; while (1) { st+=S[j]; j++; if (j==S.size()) break; if (S[j]=='+'||S[j]=='-') break; } int t=strtoint(st); ans+=t; i=j+1; ch=S[i-1]; }else { j=i; st=""; while (1) { st+=S[j]; j++; if (j==S.size()) break; if (S[j]=='+') { int t=strtoint(st); ans-=t; j++; st=""; } else if (S[j]=='-') break; } int t=strtoint(st); ans-=t; i=j+1; ch=S[i-1]; } } printf("%d\n",ans); } |
|
Total comments: 0 | |