您的位置:首页 > 其它

uva 10400 - Game Show Math

2012-09-04 19:57 357 查看
Problem H

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:

othe numbers in the expression must appear in the same order as they appear in the input file

osince 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.

oyou 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 sequencep, 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 allk 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
看到20s时间限制,就没多想按题目意思直接回溯了TLD Y-Y;看到别人有个减枝,恍然大悟,为什么理所当然的事情我一下子就没想到,可以把每次计算的结果保留下来,开一个标记数组,这样下次同一位置计算出之前的值,表明肯定无法计算出最后答案,(=

#include <stdio.h>
#include <math.h>
#include <string.h>
int f,n,b[101],ans,a[101],visit[100][64001];
void work(int sum,int deep)
{
int i,s;
if (deep>n&&sum==ans)  f=1;
if (f||deep>n||abs(sum)>32000) return;
for (i=1;i<=4;i++)
{
b[deep-1]=i;
s=32001;
if (i==1) s=sum+a[deep];
if (i==2) s=sum-a[deep];
if (i==3) s=sum*a[deep];
if (i==4&&(sum%a[deep]==0)) s=sum/a[deep];
if (abs(s)<=32000&&visit[deep-1][s]==0)
{
visit[deep-1][s]=1;
work(s,deep+1);
}
if (f) return ;
}
}
int main()
{
int i,t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&ans);
f=0;
memset(visit,0,sizeof(visit));
work(a[1],2);
if (f)
{
for (i=1;i<n;i++)
{
printf("%d",a[i]);
if (b[i]==1) printf("+");
if (b[i]==2) printf("-");
if (b[i]==3) printf("*");
if (b[i]==4) printf("/");
}
printf("%d=%d\n",a
,ans);
}
else
printf("NO EXPRESSION\n");
}
return 0;
}


O=)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: