csp 认证二十四点

    技术2022-07-11  92

    CSP 认证 二十四点

    // 先计算乘除;再计算加减,用堆栈操作是最舒服的了 // An highlighted block #include <bits/stdc++.h> using namespace std; const int N=1e5; int t1(char s[])//原理就是全部变成加号相加 { stack <int > a1; stack <char> a2; if (!a1.empty()) a1.pop(); if(!a1.empty()) a2.pop(); for(int i=0;i<7;i++) { if(s[i]>='0'&&s[i]<='9') { a1.push(s[i]-'0'); } else { if(s[i]=='+') { a2.push('+'); } if(s[i]=='-') { a1.push((s[i+1]-'0')*(-1)); //变成负的推进a1; a2.push('+'); i++; //符号后的进行跳过,先把乘除减操作了; } if(s[i]=='x') { a1.top()=a1.top()*(s[i+1]-'0'); i++; } if(s[i]=='/') { a1.top()=a1.top()/(s[i+1]-'0'); i++; } } } while(!a2.empty()) { int j=a1.top(); a1.pop(); int k=a1.top(); a1.pop(); a2.pop(); a1.push(j+k); } return a1.top(); } int main(void) { int n; char s[7 + 1]; cin>>n; int b[n]; for(int j=0;j<n;j++) { scanf("%s",s); b[j]=t1(s); } for(int j=0;j<n;j++) { if(b[j]==24) { cout<<"Yes"<<endl; } if(b[j]!=24) { cout<<"No"<<endl; } } return 0; }
    Processed: 0.009, SQL: 9