hdu 4192(運算式求值)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   2014   

題意:給一個運算式當中有一些變數,然後告訴你一些數字你可以任意排列,問能不能求出要求的結果。

思路:由於變數數目較小所以直接全排列枚舉即可,然後用棧處理運算式。

代碼如下:

 1 /************************************************** 2  * Author     : xiaohao Z 3  * Blog     : http://www.cnblogs.com/shu-xiaohao/ 4  * Last modified : 2014-06-28 21:50 5  * Filename     : hdu_4192.cpp 6  * Description     :  7  * ************************************************/ 8  9 #include <iostream>10 #include <cstdio>11 #include <cstring>12 #include <cstdlib>13 #include <cmath>14 #include <algorithm>15 #include <queue>16 #include <stack>17 #include <vector>18 #include <set>19 #include <map>20 #define MP(a, b) make_pair(a, b)21 #define PB(a) push_back(a)22 23 using namespace std;24 typedef long long ll;25 typedef pair<int, int> pii;26 typedef pair<unsigned int,unsigned int> puu;27 typedef pair<int, double> pid;28 typedef pair<ll, int> pli;29 typedef pair<int, ll> pil;30 31 const int INF = 0x3f3f3f3f;32 const double eps = 1E-6;33 const int LEN = 10100;34 int n, res, a, num[LEN], len;35 char str[LEN];36 map<char, int> mp;37 38 struct P{39     int x;40     char c;41     P(){}42     P(int _x){x = _x;}43     P(char _c){c = _c;}44 };45 46 int top;47 int ch(char c){48     if(!mp.count(c)) mp[c] = top++;49     return mp[c];50 }51 52 bool calc(){53     stack<P> s;54     for(int i=0; i<len; i++){55         if(str[i] == ‘(‘) continue;56         else if(str[i] == ‘+‘ || str[i] == ‘-‘ || str[i] == ‘*‘){57             s.push(P(str[i]));58         }else if(str[i] >= ‘a‘ && str[i] <= ‘z‘){59             s.push(P(num[ch(str[i])]));60         }else if(str[i] == ‘)‘){61             P a = s.top();s.pop();62             P op = s.top();s.pop();63             P b = s.top();s.pop();64             switch(op.c){65                 case ‘+‘: b.x+=a.x;break;66                 case ‘-‘: b.x-=a.x;break;67                 case ‘*‘: b.x*=a.x;break;68             }69             s.push(b);70         }71     }72     return s.top().x == res;73 }74 75 bool solve(){76     sort(num, num + n);77     do{78         if(calc()) return true;79     }while(next_permutation(num, num + n));80     return false;81 }82 83 int main()84 {85 //    freopen("in.txt", "r", stdin);86 87     while(scanf("%d", &n)!=EOF){88         mp.clear();top = 0;89         for(int i=0; i<n; i++)90             scanf("%d", &num[i]);91         scanf("%d", &res);92         if(!n && !res) break;93         scanf("%s", &str);94         len = strlen(str);95         if(solve()) puts("YES");96         else puts("NO");97     }98     return 0;99 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.