201903-2二十四点
生活随笔
收集整理的這篇文章主要介紹了
201903-2二十四点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<iostream>
#include<stack>
#include<ctype.h>
#include<string>
using namespace std;
struct oprt
{char opr;int clas;//運算符等級
};
oprt op[5] = { {'#',0}, {'+',1},{'-',1},{'x',3},{'/',3} };
int judge(char c)
{for (int i = 0; i < 5; i++){if (op[i].opr == c)return op[i].clas;}return -1;
}
int main()
{int n;cin >> n;for (int i = 0; i < n; i++){string s;cin >> s;stack<int> nu;stack<char> opr;opr.push('#');for (int j = 0; j < 7; j++)//將計算表達式壓入棧中,要求已有符號若大于待入符號,則先運算已有符號即可,否則壓入{if (isdigit(s[j])){nu.push(s[j]-'0');}else{if (judge(s[j]) > judge(opr.top())){opr.push(s[j]);}else{char ct = opr.top();opr.pop();int t2 = nu.top();nu.pop();int t1 = nu.top();nu.pop();switch (ct){case '+':nu.push(t1 + t2); break;case '-':nu.push(t1 - t2); break;case 'x':nu.push(t1 * t2); break;case '/':nu.push(t1 / t2); break;}opr.push(s[j]);}}}//最終形成的符號棧總下到上等級提高while (nu.size() > 1){char ct = opr.top();opr.pop();int t2 = nu.top();nu.pop();int t1 = nu.top();nu.pop();switch (ct){case '+':nu.push(t1 + t2); break;case '-':nu.push(t1 - t2); break;case 'x':nu.push(t1 * t2); break;case '/':nu.push(t1 / t2); break;}}if (nu.top() == 24)cout << "YES" << endl;elsecout << "NO" << endl;}return 0;
}
轉載于:https://www.cnblogs.com/WuDie/p/11327889.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的201903-2二十四点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Spring DI之旅
- 下一篇: Production Hair Rend