uva 10400(Game Show Math) (DP)
来源:互联网 发布:mac安装win10不能更新 编辑:程序博客网 时间:2024/05/21 12:42
Game Show Math
Input: standard input
Output: standard output
Time Limit: 15 seconds
A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators:+, -, *, and, /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is possible that no expression can generate the target number. It is possible that many expressions can generate the target number.
There are three restrictions on the composition of the mathematical expression:
o the numbers in the expression must appear in the same order as they appear in the input file
o since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.
o you are only allowed to use an operator in the expression, if its result after applying that operator is an integer from (-32000 ..+32000).
Input
The input file describes multiple test cases. The first line contains the number of test casesn.
Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that0 < p � 100. There may be duplicate numbers in the sequence. But all the numbers are less than32000.
Output
The output file should contain an expression, including all k numbers and(k-1) operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here. If there is no expression possible output"NO EXPRESSION" (without the quotes). If more than one expression is possible, any one of them will do.
Sample Input
3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4
Sample Output
5+7/4=3
NO EXPRESSION
12-2/5*1*2=4
#include <iostream>#include <cstdio>#include <vector>using namespace std;const int maxn = 64060;int dp[110][maxn];int p , aim;vector<int> num;vector<int> OP;char c[5] = {'0' , '/' , '*' , '-' , '+'};void initial(){for(int i = 0;i < 110;i++){for(int k = 0;k < maxn;k++){dp[i][k] = 0;}}num.clear();OP.clear();}void readcase(){cin >> p;for(int i = 0;i < p;i++){int n;cin >> n;num.push_back(n);}dp[0][num[0]+32000] = 1;cin >>aim;}void computing(){aim += 32000;for(int i = 1;i < p;i++){for(int k = 0;k < maxn;k++){if(dp[i-1][k] != 0){int temp = k-32000;//cout << i-1 << ":" << temp << endl;//cout << temp << endl;for(int op = 1;op <= 4;op++){int tt = 0;if(op == 1 && temp%num[i] == 0){tt = temp/num[i];dp[i][tt+32000] = op;//cout << i << " : " << tt << " " << op << endl;}else if(op == 2 && temp*num[i] < 32000 && temp*num[i] > -1*32000){tt = temp*num[i];dp[i][tt+32000] = op;//cout << i << " : " << tt << " " << op << endl;}else if(op == 3 && temp-num[i] > -1*32000){tt = temp-num[i];dp[i][tt+32000] = op;//cout << i << " : " << tt << " " << op << endl;}else if(op == 4 && temp+num[i] < 32000){tt = temp+num[i];dp[i][tt+32000] = op;//cout << i << " : " << tt << " " << op << endl;}}}}}}void out(){int op = dp[p-1][aim];if(op == 0){cout << "NO EXPRESSION" << endl;}else{int k = p-1 , temp = aim;while(k >= 1){OP.push_back(op);temp -= 32000;if(op == 1){temp = temp*num[k];}else if(op == 2){temp = temp/num[k];}else if(op == 3){temp = temp+num[k];}else{temp = temp-num[k];}temp += 32000;op = dp[--k][temp];}cout << num[0];for(int i = 1;i < p;i++){cout << c[OP[OP.size()-i]] << num[i];}cout << "=" << aim-32000 << endl;}}int main(){int t;cin >> t;while(t--){initial();readcase();computing();out();}return 0;}
- uva 10400(Game Show Math) (DP)
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVa 10400 - Game Show Math
- UVA 10400 - Game Show Math
- uva 10400 Game Show Math
- uva 10400 Game Show Math
- UVA 10400 Game Show Math
- uva 10400 Game Show Math
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVA - 10400 Game Show Math
- UVa 10400 - Game Show Math
- UVA - 10400 Game Show Math
- Uva 10400 Game Show Math (DP+记录路径)
- UVA - 10400 Game Show Math(回溯)
- UVA - 10400 Game Show Math DFS
- 程序员应该看的11本书
- Hibernate的检索策略
- 在使用uiwebview时出现一个奇怪现象
- 《参禅与悟道》——浅谈人生
- Mac下搭建Sublime+lua开发环境
- uva 10400(Game Show Math) (DP)
- 【机器学习实战之二】:C++实现基于概率论的分类方法--朴素贝叶斯分类(Naive Bayes Classifier)
- Gallery的介绍与使用
- mysql 中decimal中去掉后面多余的0
- 存储过程
- java_easyui体系之DataGrid(2)
- Object-C. _cmd用法
- Android自定义属性实例
- C++ STL--list双向链表容器学习笔记一