uva10400

Problem H
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 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 ppositive 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

确定先确定所有可能值,再进行广度优先搜索。时间复杂度O(32000*2*100);

#include<cstdio>
#include<cstring>
#define N 32000
#define E 6400000
int q[E],m,s[108],ok,n,M[E],D[E];
char F[E],S[108];
int bfs()
{
    int rear=0,front=0;
    q[rear++]=(s[0]+N)*100;
    while(front<rear)
    {
        int u=q[front++];
        int a=u/100-N,i=u%100+1,l;
        if(i<n)
        {
            if(a*s[i]<N&&a*s[i]>-N)
            {
                l=(a*s[i]+N)*100+i;
                if(!M[l])
                {
                    M[l]=1;
                    D[l]=u;
                    F[l]='*';
                    q[rear++]=l;
                }
            }
            if(!(a%s[i]))
            {
                l=(a/s[i]+N)*100+i;
                if(!M[l])
                {
                    M[l]=1;
                    D[l]=u;
                    F[l]='/';
                    q[rear++]=l;
                }
            }
            if(a+s[i]<N)
            {
                l=(a+s[i]+N)*100+i;
                if(!M[l])
                {
                    M[l]=1;
                    D[l]=u;
                    F[l]='+';
                    q[rear++]=l;
                }
            }
            if(a-s[i]>-N)
            {
                l=(a-s[i]+N)*100+i;
                if(!M[l])
                {
                    M[l]=1;
                    D[l]=u;
                    F[l]='-';
                    q[rear++]=l;
                }
            }
        }
        else
        {
            if(a==m)
            {

                int o=1,c=D[u];
                S[0]=F[u];
                while(o<n-1)
                {
                    S[o++]=F[c];
                    c=D[c];
                }
                printf("%d",s[0]);
                for(int i=1; i<n; i++)
                    printf("%c%d",S[n-i-1],s[i]);
                printf("=%d\n",m);
                ok=1;
                return 0;
            }
        }
    }
}
int main()
{
    // freopen("1.txt","r",stdin);
    int num;
    scanf("%d",&num);
    while(num--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&s[i]);
        scanf("%d",&m);
        ok=0;
        memset(M,0,sizeof(M));
        bfs();
        if(!ok)
        {
            printf("NO EXPRESSION\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值