24点游戏

题目描述:
给出4个1 - 10的数字,通过加减乘除,得到数字为24就算胜利
输入:
4个1 - 10的数字。[数字允许重复,但每个数字仅允许使用一次,测试用例保证无异常数字],通过加减乘除,得到数字为24就算胜利
输出:
true or false

思路一:

*整理一下整个运算式子是 num1 o num2 o num3 o num4 (o的地方填运算符)
利用全排列,将四个位置上的数进行组合
o的位置可以是+ -
/ 任意那么就是挨个循环出所有的可能性**

代码:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N=4;
int num[N];
int isSolve=0;
void dfs(int index,double currentNum,int arr[])
{
    if (index >= 4){
        if (currentNum == 24)
            isSolve = 1;
        return;
    }

    for(int operFlag=0;operFlag<4;operFlag++)
    {
        switch(operFlag)
        {
        case 0:
            dfs(index+1,currentNum+arr[index],arr);
            break;
        case 1:
            dfs(index+1,currentNum-arr[index],arr);
            break;
        case 2:
            dfs(index+1,currentNum*arr[index],arr);
            break;
        case 3:
            dfs(index+1,currentNum/arr[index],arr);
            break;
        }
        if(isSolve)
            return;
    }
}
int main()
{
    while (cin>>num[0]>>num[1]>>num[2]>>num[3])
    {
        isSolve = 0;
        sort(num, num + 4);
        do
        {
            dfs(1, num[0], num);
            if (isSolve)
                break;
        } while (next_permutation(num, num + 4));   //全排列函数
        if (isSolve)
            printf("true\n");
        else
            printf("false\n");
    }
    return 0;
}

全排列函数介绍:包含头文件<algorithm>,全排列的数组一定要是有序的,所以使用前一定要排序,否则会只按照当前的大小按字典序逐个增加的排序
使用栗子
对一个数组全排列

#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
    int n;
    while (scanf("%d", &n) && n){
        int a[1000];
        for (int i = 0; i<n; i++){
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
        do{
            for (int i = 0; i<n; i++)
                printf("%d ", a[i]);
            printf("\n");
        } while (next_permutation(a, a + n));
    }
    return 0;
}

思路二:

开辟一个数组,每次用完一个数据擦掉一个。
一个for循环表示挨个擦去数组中的数据并拿这个数据进行运算;然后用递归的形式将这个删过数据的元素的数组作为下一轮递归的参数,作为下一轮选取数的数组,直到这个数组中的数全部被取完,就结束。
代码:

bool CALL(vector<int> v, double res){
    if (v.empty()){
        return res == 24;
    }
    for (int i = 0; i<v.size(); i++){
        vector<int> tmp(v);
        tmp.erase(tmp.begin() + i);
        if (CALL(tmp, res + v[i]) || CALL(tmp, res*v[i]) || CALL(tmp, res - v[i]) || CALL(tmp, res / v[i])){
            return true;
        }
        return false;

    }
}
int main(){
    vector<int> v(4);
    while (cin >> v[0]){
        for (int i = 1; i<v.size(); i++){
            cin >> v[i];
        }

        if (CALL(v, 0)){
            cout << "true" << endl;
        }
        else
            cout << "false" << endl;
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章