链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1341
类型:回溯
原题:
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 cases n.
Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that 0 < p 100. There may be duplicate numbers in the sequence. But all the numbers are less than 32000.
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
题目大意:
给一些数字和一个目标数字,任意使用+,-,*,/四个符号计算(在这里四个符号的优先级都是相同的),是这些数字表达式的结果为目标数字。
分析与总结:
本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/
这中题目最适合用回溯来做了,但是看题目的数据量估计吓了不少人不敢用回溯。用回溯0.084s水过...
代码:
/* * UVa: 10400 - Game Show Math * Type: dfs * Time: 0.084s * Author: D_Double * */ #include<iostream> #include<cstring> #include<cstdio> #define ADD 32000 using namespace std; int arr[120], target, p; char route[120]; bool flag, vis[102][32002*2+2]; inline bool isOk(int m, int cur){ return m>=-32000&&m<=32000 && !vis[cur][m+ADD]; } void dfs(int cur, int sum){ if(flag)return ; if(cur==p){ if(sum==target) flag=true; return; } if(flag) return; for(int i=0; i<4; ++i){ if(i==0&&isOk(sum+arr[cur], cur)){ vis[cur][sum+arr[cur]+ADD] = true; route[cur-1]='+'; dfs(cur+1, sum+arr[cur]); } else if(i==1 && isOk(sum-arr[cur], cur)){ vis[cur][sum-arr[cur]+ADD] = true; route[cur-1]='-'; dfs(cur+1,sum-arr[cur]); } else if(i==2 && isOk(sum*arr[cur], cur)){ vis[cur][sum*arr[cur]+ADD] = true; route[cur-1]='*'; dfs(cur+1,sum*arr[cur]); } else if(i==3 && arr[cur]!=0 && isOk(sum/arr[cur], cur)){ vis[cur][sum/arr[cur]+ADD] = true; route[cur-1]='/'; dfs(cur+1,sum/arr[cur]); } if(flag)return; } } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&p); for(int i=0; i<p; ++i) scanf("%d",&arr[i]); scanf("%d",&target); memset(vis, false, sizeof(vis)); vis[0][arr[0]+ADD] = true; flag=false; dfs(1, arr[0]); if(flag){ printf("%d",arr[0]); for(int i=1; i<p; ++i){ printf("%c%d",route[i-1],arr[i]); } printf("=%d\n",target); } else printf("NO EXPRESSION\n"); } return 0; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索numbers
, number
, target
, p has no constructo
, expression
, sequence
The
小米移动电源10400、小米充电宝10400、小米10400mah移动电源、小米充电宝10400毫安、爱国者ol10400拆解,以便于您获取更多的相关知识。