uva 10400 Game Show Math

来源:互联网 发布:bim软件分类 编辑:程序博客网 时间:2024/06/07 15:21
Game Show Math
Input:
standard input
Output: standard output
Time Limit: 15 seconds

A game show in Britainhas a segment where it gives its contestants a sequence of positive numbers anda target number. The contestant must make a mathematical expression using allof the numbers in the sequence and only the operators:+, -, *, and, /. Each number in the sequence must be used exactly once, but eachoperator may be used zero to many times. The expression should be read fromleft to right, without regard for order of operations, to calculate the targetnumber. It is possible that no expression can generate the target number. It ispossible that many expressions can generate the target number.

There are three restrictions on the compositionof the mathematical expression:

o the numbers in the expression must appear in thesame 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 theresult will give a remainder of zero.

o you are only allowed touse an operator in the expression, if its result after applying that operatoris an integer from (-32000 ..+32000).

Input

The input file describes multiple test cases. Thefirst line contains the number of test casesn.

Each subsequent line contains the number ofpositive numbers in the sequence p, followed by ppositive numbers, followed by the target number. Note that0 < p £100. There may be duplicate numbers in the sequence. But all the numbers areless than32000.

Output

The output file should contain anexpression, including all k numbers and(k-1) operators plus the equals sign and the target. Do notinclude spaces in your expression. Remember that order of operations does notapply here. If there is no expression possible output"NO EXPRESSION"(without the quotes). If more than one expression is possible, anyone 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


题意:

给出n个数,要求不改变其顺序,然后用‘+’ ,‘-’ ,‘ * ’, ‘/ '四种运算符自由组合得到最后一个结果如果得不到结果就输出“NO EXPRESSION”;

解题思路:

很自然的想到了搜索,如果你暴力求的话,肯定超时;深搜还要考虑一个问题,那就是如何判断该状态是否出现过,所以用到了枝剪方法来判断是否出现该种状态

代码:

#include<iostream>#include<stdio.h>#include<string>#include<queue>#include<string.h>#include<cmath>#include<algorithm>#include<math.h>using namespace std;int a[150],n,sum;bool vis[150][32000<<1+5],bigon;//因为和的值在-32000到32000之间要保证数组的下标为正数所以要这么大的数组string ans;//深搜中的所有符号bool cut(int a,int b)//枝剪函数{    if(b>=-32000&&b<=32000&&!vis[a][32000+b])    {        vis[a][b+32000]=1;        return true;    }    return false;}void dfs(int con,int sun,string s){    if(bigon)        return;    if(con==n)//如果深搜完成    {        if(sun==sum)//值相等        {            bigon=1;//(x)            for(int i=0;i<n-1;i++)            {                cout<<a[i]<<s[i];            }            cout<<a[n-1]<<'='<<sum<<endl;        }        return;    }    else    {        if(cut(con+1,sun+a[con+1]))        {            dfs(con+1,sun+a[con+1],s+'+');        }        if(cut(con+1,sun-a[con+1]))        {            dfs(con+1,sun-a[con+1],s+'-');        }        if(cut(con+1,sun*a[con+1]))        {            dfs(con+1,sun*a[con+1],s+'*');        }        if(a[con+1]!=0&&sun%a[con+1]==0&&cut(con+1,sun/a[con+1]))//(x)出错原因,没考虑先后序列,需要先保证a[con+1]!=0,才可以保证cut函数            dfs(con+1,sun/a[con+1],s+'/');    }}int main(){    int t;    cin>>t;    while(t--)    {        memset(vis,0,sizeof(vis));        memset(a,0,sizeof(a));        cin>>n;        for(int i=0;i<n;i++)        {            cin>>a[i];        }        cin>>sum;        bigon=0;        dfs(0,a[0],ans);        if(!bigon)            cout<<"NO EXPRESSION"<<endl;    }}


0 0
原创粉丝点击