您的位置:首页 > 其它

Uva 10400 Game Show Math (DP+记录路径)

2014-10-19 23:55 531 查看
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:

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

题意就不说了,看样例就应该懂了。

思路: dp[ i ][ j ] 表示运算 到 第 i 个数 是的数值为 j 的前面一个运算符是什么 。

然后 四种状态 : + , - ,* , / 分别考虑下就ok (具体细节看代码)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=32000;

int dp[105][maxn*2+10];
int n,a[105],m;

void input()
{
scanf("%d",&n);
for(int i=0; i<n; i++)  scanf("%d",&a[i]);
scanf("%d",&m);
m+=maxn;
memset(dp,-1,sizeof(dp));
}

bool judge(int t)
{
if(t<=maxn && t>=-maxn)  return true;
return false;
}

void print(int m,int cnt)
{
if(cnt==0)
{
printf("%d",a[0]);
return ;
}
int k=m-maxn;
if(dp[cnt][m]==1)  print(k-a[cnt]+maxn,cnt-1),putchar('+');
else if(dp[cnt][m]==2)  print(k+a[cnt]+maxn,cnt-1),putchar('-');
else if(dp[cnt][m]==3)  print(k/a[cnt]+maxn,cnt-1),putchar('*');
else if(dp[cnt][m]==4)  print(k*a[cnt]+maxn,cnt-1),putchar('/');
printf("%d",a[cnt]);
}

void solve()
{
dp[0][a[0]+maxn]=0;
int t;
for(int i=1; i<n; i++)
for(int j=0; j<=2*maxn; j++)
{
if(dp[i-1][j]!=-1)
{
int k=j-maxn;
t=k+a[i];  if(judge(t))  dp[i][t+maxn]=1;
t=k-a[i];  if(judge(t))  dp[i][t+maxn]=2;
t=k*a[i];  if(judge(t))  dp[i][t+maxn]=3;
if(a[i]!=0 && k%a[i]==0)
{
t=k/a[i];
if(judge(t))  dp[i][t+maxn]=4;
}
}
}
if(dp[n-1][m]==-1)  printf("NO EXPRESSION\n");
else
{
print(m,n-1);
printf("=%d\n",m-maxn);
}
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
input();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: