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 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
题意:用给定的数字使用+-*/来得到最后一个数。
思路:因为要求中间数值在+-3200之内,所以用搜索的方法就可以了。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
int T,t,n,vis[110][64010],pre[110][54010],val[110],m=32000;
bool flag;
void dfs(int pos,int fa,int num,int type)
{
if(num<=-m || num>=m)
return ;
if(vis[pos][num+m] || flag)
return;
vis[pos][num+m]=type;
pre[pos][num+m]=fa;
if(pos==n)
{
if(num==val[n+1])
flag=true;
return;
}
int num2;
num2=num+val[pos+1];dfs(pos+1,num,num2,1);
num2=num-val[pos+1];dfs(pos+1,num,num2,2);
num2=num*val[pos+1];dfs(pos+1,num,num2,3);
num2=num/val[pos+1];dfs(pos+1,num,num2,4);
}
void print(int pos,int num)
{
int num2=num+m;
if(pos>1)
print(pos-1,pre[pos][num2]);
if(vis[pos][num2]==1)
printf("+");
else if(vis[pos][num2]==2)
printf("-");
else if(vis[pos][num2]==3)
printf("*");
else if(vis[pos][num2]==4)
printf("/");
printf("%d",val[pos]);
}
int main()
{
int i,j,k;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d",&n);
for(i=1;i<=n+1;i++)
scanf("%d",&val[i]);
memset(vis,0,sizeof(vis));
flag=false;
dfs(1,0,val[1],0);
if(flag)
{
print(n,val[n+1]);
printf("=%d\n",val[n+1]);
}
else
printf("NO EXPRESSION\n");
}
}