uva 10344 - 23 out of 5

点击打开链接

题目意思:给定5个数,还有三种运算符 * + - ,问我们是否能够由这些数和运算符最后的值为23。

解题思路:我们知道5个数的全排列为5!种,那么我们只要去枚举这个全排列中每一个排列进行搜索,是否有23点出现有的话标记ans为1,直接退出。(注意必须全排列的查找,不能只按输入的顺序)

代码:

//计算5个数3个运算符能否组成23点
//我们知道5个数的全排列为5!种,那么我们只要去枚举这个全排列中每一个排列进行搜索,是否有23点出现有的话标记ans为1,直接退出。(注意必须全排列的查找,不能只按输入的顺序)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

int ans;
int num[5];
char ope[3] = {'*' , '+' , '-'};//运算符数组

//搜索
void dfs(int k , int sum){
    if(k >= 5){//如果计算完成,判断是否有23点出现
        if(sum == 23)
            ans = 1;
        return;
    }
    if(k < 5){
        for(int i = 0 ; i < 3 ; i++){//搜索三种符号
            //三种情况回溯,注意现场的恢复
            if(i == 0){
                sum *= num[k];
                dfs(k+1 , sum);
                sum /= num[k];
            }
            if(i == 1){
                sum += num[k];
                dfs(k+1 , sum);
                sum -= num[k];
            }
            if(i == 2){
                sum -= num[k];
                dfs(k+1 , sum);
                sum += num[k];
            }
        }
        ++k;//下一个数
    }
}

//
int main(){
    int sum;
    while(1){
        sum = 0;
        ans = 0;
        for(int i = 0 ; i < 5 ; i++){
            scanf("%d" , &num[i]);
            sum += num[i];
        }
        if(sum == 0) break;//全为0直接退出
        else{
            sort(num , num+5);//先排序
            dfs(1 , num[0]);//求一下第一个顺序
            if(ans)
                printf("Possible\n");
            else{
                while(next_permutation(num , num+5)){//全排列
                    dfs(1 , num[0]);
                    if(ans){//搜索到了,直接退出
                        break;
                    }
                }
                if(ans) printf("Possible\n");
                else    printf("Impossible\n");
            }
        }
    }
    return 0;
}
时间: 2025-01-30 17:32:45

uva 10344 - 23 out of 5的相关文章

UVa 10344 23 out of 5:全排列枚举&amp;amp;回溯

10344 - 23 out of 5 Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1285 Your task is to write a program that can decide whether you can find an arithmeti

UVa 10344:23 out of 5, 智力小游戏:算23点

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=1285 类型: 回溯 原题: Your task is to write a program that can decide whether you can find an arithmetic expression consisting of f

UVa 10714:Ants

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1655 原题: An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant rea

uva 10688:The Poor Giant(区间dp)

题目链接: uva-10688 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=514&page=show_problem&problem=1629 题意 有n个苹果,和一个数k,第i个苹果的重量是k+i(1<=i<=n). 已知其中只有一个苹果是甜的, 所有比它重量轻的都是苦的,比它重的都是酸的. 为了要找出甜的苹果,就要去一个一个地吃它,且吃了咬了苹果

UVa 624 CD (0-1背包)

624 - CD Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=565 You have a long drive by car ahead. You have a tape recorder, but unfortunately your best mus

UVa 10130 SuperSale (0-1背包)

10130 - SuperSale Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1071 There is a SuperSale in a SuperHiperMarket. Every person can take only one object o

UVa 402 M*A*S*H (STL&amp;amp;list)

402 - M*A*S*H Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=343 Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean W

UVa 10205 Stack &#039;em Up (模拟)

10205 - Stack 'em Up Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1146 A standard playing card deck contains 52 cards, 13 values in each of four suits.

UVa 543 Goldbach&#039;s Conjecture:素数&amp;amp;哥德巴赫猜想

543 - Goldbach's Conjecture Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=484 In 1742, Christian Goldbach, a German amateur mathematician, sent a letter