http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4844
题意:给你一个只包含中括号和正整数,+,-,结果在longlong范围内的公式和两个备选答案,
如果答案中有9,第一个是9选A,否则选B
否则,如果第一个是正确答案,输出B,否则输出A
#include <cstdio> #include <iostream> #include <cstring> #include <cctype> #define clr(x,y) memset(x, y, sizeof x) #include <cmath> using namespace std; const int maxn=2e3+3; typedef long long ll; char maz[maxn]; long long read(int& head){ //printf("read start from %d ",head); long long ans=0; while(isalnum(maz[head])){ ans*=10; ans+=maz[head]-'0'; head++; } // printf("end at %d ans = %lld ",head,ans); return ans; } typedef pair<long long ,int > p; p calc(int st){ long long ans=0; int cnt=1,ind =st; for(;maz[ind]!=0&&maz[ind]!=']';){ if(maz[ind]=='-'){ cnt=-1; ind ++; //printf("calc ind %d cnt -1 ",ind); } else if(maz[ind]=='+'){ ind++; } else if(isalnum(maz[ind])){ ans+=cnt*read(ind); cnt=1; //printf("calc ind %d cnt 1 ans%lld ",ind,ans); } else if(maz[ind]=='['){ ind++; p tmp=calc(ind); ans+=cnt*tmp.first; ind=tmp.second+1; cnt=1; } } //printf("calc start at %d end at %d ans = %lld ",st,ind,ans); return p(ans,ind); } int main(){ //freopen("input.txt","r",stdin); while(scanf("%s",maz)==1){ long long a,b; scanf("%lld%lld",&a,&b); if(a==9){ puts("A"); continue; } else if(b==9){ puts("B"); continue; } long long ans=calc(0).first; if(ans==a){ puts("B"); } else puts("A"); } return 0; }