ccf 201903-2 二十四点 (100分)

mac2026-08-14  6

 

提交后得100分的C++程序如下:

#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n--){ string str; cin>>str; stack<int> tmp;//运算数 stack<char> op;//运算符 for(int i=0;i<7;i++) { if(str[i]>='1'&&str[i]<='9') { tmp.push(str[i]-'0'); } else if(str[i]=='+'||str[i]=='-') { while(!op.empty()){ int b=tmp.top(); tmp.pop(); int a=tmp.top(); tmp.pop(); char opp=op.top(); op.pop(); if(opp=='+'){ tmp.push(a+b); } else{ tmp.push(a-b); } } op.push(str[i]); } else{ int b=str[i+1]-'0'; int a=tmp.top(); tmp.pop(); if(str[i]=='x'){ tmp.push(a*b); } else tmp.push(a/b); i++; } } while(!op.empty()){ int b=tmp.top(); tmp.pop(); int a=tmp.top(); tmp.pop(); char opp=op.top(); op.pop(); if(opp=='+'){ tmp.push(a+b); } else{ tmp.push(a-b); } } if(tmp.top()==24) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }

 

最新回复(0)