UVa 10400 - Game Show Math

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1341


类型:回溯


原题:

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


题目大意:

给一些数字和一个目标数字,任意使用+,-,*,/四个符号计算(在这里四个符号的优先级都是相同的),是这些数字表达式的结果为目标数字。


分析与总结:

这中题目最适合用回溯来做了,但是看题目的数据量估计吓了不少人不敢用回溯。用回溯0.084s水过...



代码:


/*
 * UVa: 10400 - Game Show Math
 * Type: dfs
 * Time: 0.084s
 * Author: D_Double
 *
 */
#include<iostream>
#include<cstring>
#include<cstdio>
#define ADD 32000
using namespace std;

int arr[120], target, p;
char route[120];
bool flag, vis[102][32002*2+2];

inline bool isOk(int m, int cur){
    return m>=-32000&&m<=32000 && !vis[cur][m+ADD];
}

void dfs(int cur, int sum){
    if(flag)return ;
    if(cur==p){
        if(sum==target) flag=true;
        return;
    }
    if(flag) return;

    for(int i=0; i<4; ++i){
        if(i==0&&isOk(sum+arr[cur], cur)){
            vis[cur][sum+arr[cur]+ADD] = true;
            route[cur-1]='+';
            dfs(cur+1, sum+arr[cur]);
        }
        else if(i==1 && isOk(sum-arr[cur], cur)){
            vis[cur][sum-arr[cur]+ADD] = true;
            route[cur-1]='-';
            dfs(cur+1,sum-arr[cur]);
        }
        else if(i==2 && isOk(sum*arr[cur], cur)){
            vis[cur][sum*arr[cur]+ADD] = true;
            route[cur-1]='*';
            dfs(cur+1,sum*arr[cur]);
        }
        else if(i==3 && arr[cur]!=0 && isOk(sum/arr[cur], cur)){
            vis[cur][sum/arr[cur]+ADD] = true;
            route[cur-1]='/';
            dfs(cur+1,sum/arr[cur]);
        }
        if(flag)return;
    }
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&p);
        for(int i=0; i<p; ++i)
            scanf("%d",&arr[i]);
        scanf("%d",&target);

        memset(vis, false, sizeof(vis));
        vis[0][arr[0]+ADD] = true;
        flag=false;
        dfs(1, arr[0]);

        if(flag){
            printf("%d",arr[0]);
            for(int i=1; i<p; ++i){
                printf("%c%d",route[i-1],arr[i]);
            }
            printf("=%d\n",target);
        }
        else 
            printf("NO EXPRESSION\n");
    }
    return 0;
} 


——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值