2019-3-2 二十四点
问题描述
试题编号: | 201903-2 |
试题名称: | 二十四点 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: |
|
/* 10 9+3+4x3 5+4x5x5 7-9-9+8 5x6/5x4 3+5+7+9 1x1+9-9 1x9-5/9 8/5+6x9 6x7-3x6 6x4+4/5 */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<stack> using namespace std; stack<int > num; stack <char > opt; char s[10]; void judge (char *s) { for(int i=0;i<7;i++) { if(s[i]>='0' && s[i]<='9') { if(opt.empty()) { num.push(s[i]-'0'); } else { if(opt.top()=='+')// { num.push(s[i]-'0'); } else if(opt.top()=='-')//减去x转换为加-x { num.push(-(s[i]-'0')); opt.top()='+'; } else if(opt.top()=='x')//x { int a=num.top(); num.pop(); int b=s[i]-'0'; opt.pop(); num.push(a*b); } else if(opt.top()=='/') { int a=num.top(); num.pop(); int b=s[i]-'0'; opt.pop(); num.push(a/b);//整除 } } } else { opt.push(s[i]); } } /*while(!num.empty())//测试,输出数字栈 { printf("%d ",num.top()); num.pop(); } printf("\n"); while(!opt.empty()) { printf("%c ",opt.top()); opt.pop(); } printf("\n");*/ } int jisuan() { int sum=0; while(!opt.empty()) { if(opt.top()=='+') { int a=num.top(); num.pop(); int b=num.top(); num.pop(); opt.pop(); num.push(a+b); } else if(opt.top()=='-') { int a=num.top(); num.pop(); int b=num.top(); num.pop(); opt.pop(); num.push(b-a); } } sum=num.top(); return sum; } int main () { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",s); //printf("1\n"); judge(s); //printf("2\n"); int sum=jisuan(); //printf("%d ",sum); if(sum==24) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
本文来自博客园,作者:永恒&,转载请注明原文链接:https://www.cnblogs.com/Sun-yuan/p/14455942.html