UVa 10400

链接:

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

题目大意:

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

分析与总结:

本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/

这中题目最适合用回溯来做了,但是看题目的数据量估计吓了不少人不敢用回溯。用回溯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;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索numbers
, number
, target
, p has no constructo
, expression
, sequence
The
小米移动电源10400、小米充电宝10400、小米10400mah移动电源、小米充电宝10400毫安、爱国者ol10400拆解,以便于您获取更多的相关知识。

时间: 2024-12-26 18:27:15

UVa 10400的相关文章

uva 10400 - Game Show Math

点击打开链接uva 10400 题目意思:     给定n个数和一个目标数,问我们能否找到一个表达式使得这n个数算出来最后的结果等于这个目标值,注意这里的所有运算的优先级一样,都是从左向右的. 解题思路:     1:思路:DFS+状态判重                        2:由于这一题的时间给了20s,那么DFS是没问题的.我们只要从第一个开始进行DFS,然后每一次当前是否满足一下条件:1 当前和-32000<=sum<=32000    2当前的状态vis[i][sum]没有

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic