本文主要是介绍HDU-4192 Guess the Numbers 表达式求值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给出n个数字,能不能通过所给表达式计算得到m。比较水的题,可能代码写起来比较麻烦。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int ss[26];
char s[1111];
int d[10];
int n, m;
int u = 0;
int cul() {int r = 0, p, p1, p2;if(s[u] == '(') {u++;r = cul();}else {p = ss[s[u] - 'a'];r = d[p];u++;}for(;s[u];) {if(s[u] == ')') {u++;return r;}int t;char ch = s[u];u++;if(s[u] == '(') {u++;t = cul();}else {p = ss[s[u] - 'a'];t = d[p];u++;}if(ch == '*')r *= t;if(ch == '+')r += t;if(ch == '-')r -= t;}return r;
}
int deal() {int is = 0;memset(ss, -1, sizeof(ss));for(int i = 0; s[i]; i++) {if(islower(s[i]) && ss[s[i] - 'a'] == -1)ss[s[i] - 'a'] = is++;}do {u = 0;if(cul() == m)return 1;}while(next_permutation(d, d + n));return 0;
}
int main() {int i, j;while(~scanf("%d", &n)) {for(i = 0; i < n; i++)scanf("%d", &d[i]);scanf("%d", &m);if(!n && !m)break;scanf("%s", s);sort(d, d + n);if(deal())printf("YES\n");else printf("NO\n");}return 0;
}
这篇关于HDU-4192 Guess the Numbers 表达式求值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!