24点游戏

给你4张牌 A-K  即[1,13] , 可以任意四种基本操作 + - * / 和 ( )  优先级     判断是否可以计算得到24

hits: 只会暴力解法, 4张牌  a,b,c,d  对应的组合一共有 4! = 24种

         都是2目操作符 ,因此只需要3种操作, 一共有 4*4*4 = 64种

         关于优先级,每次只有一对操作优先 包含2个操作数和一个操作符 ,因此,枚举所有优先情况也只有以下5种:包含逆波兰表达式(后缀表达式)

   1. ((ab)c)d   00+0+0+      

 2. (a(bc))d   000++0+ 

 3. (ab)(cd)   00+00++

   4. a((bc)d)   000+0++

   5. a(b(cd))   0000+++

   因此一种需要枚举的情况是 24* 64*5 种  数量不算很多, 可以考虑枚举所有后缀表达式,用表达式来求解。

具体求解过程: 对表达式由左向右遍历,用一个栈, 遇到操作数则压栈,遇到操作符,弹出两个栈顶元素求值,求值结束后压栈。

代码太乱,懒得改了..大家也别看了... =。=

//
//#include <stdlib.h>
//#include <stdio.h>
//#include <cv.h>
//#include <highgui.h>
#include <string>
#include <iostream>
#include <math.h>
//#include <fstream>
//#include <Windows.h>
#include <algorithm>
#include <time.h>
//#include <set>
//#include <list>
//#include <stack>
using namespace std;


int ar[30][4];
int total = 0;
void dfs(int deep,int arr[],int len)             
{
    if (deep == len)
    {
        for (int i = 0;i < len;++i)
        {
            ar[total][i] = arr[i];
        }
        ++total;
        return;
    }
    dfs(deep + 1,arr,len);
    for (int i = deep + 1;i < len;++i)
    {
        swap(arr[deep],arr[i]);
        dfs(deep + 1,arr,len);
        swap(arr[deep],arr[i]);
    }
}

inline double jisuan(double x1,double x2,int f)
{
    switch (f)
    {
    case 1:
        return x1+x2;
    case 2:
        return x1-x2;
    case 3:
        return x1*x2;
    case 4:
        if (x2 != 0)
        {
            return x1/x2;
        }
        else
            return 0x7f;
    }
}
double matht(int num[],int f[],int ca)
{
    double sum,temp;
    switch (ca)
    {
    case 0:
        sum = jisuan(num[0],num[1],f[0]);
        sum = jisuan(sum,num[2],f[1]);
        sum = jisuan(sum,num[3],f[2]);
        break;
    case 1:
        sum = jisuan(num[1],num[2],f[0]);
        sum = jisuan(num[0],sum,f[1]);
        sum = jisuan(sum,num[3],f[2]);
        break;
    case 2:
        sum = jisuan(num[0],num[1],f[0]);
        temp = jisuan(num[2],num[3],f[1]);
        sum = jisuan(sum,temp,f[2]);
        break;
    case 3:
        sum = jisuan(num[1],num[2],f[0]);
        sum = jisuan(sum,num[3],f[1]);
        sum = jisuan(num[0],sum,f[2]);
        break;
    case 4:
        sum = jisuan(num[2],num[3],f[0]);
        sum = jisuan(num[1],sum,f[1]);
        sum = jisuan(num[0],sum,f[2]);
        break;
    }
    return sum;
}
char toCh(int f)
{
    switch (f)
    {
    case 1:
        return '+';
    case 2:
        return '-';
    case 3:
        return '*';
    case 4:
        return '/';
    }
}

int main()
{
    int fh[68][3];
    int fh_num = 0;
    char str[30];
    double sum;
    bool isok = false;
    for (int i = 0;i < 4;++i)
    {
        for (int j = 0;j < 4;++j)
        {
            for (int k = 0 ; k < 4;++k)
            {
                fh[fh_num][0] = i + 1;
                fh[fh_num][1] = j + 1;
                fh[fh_num++][2] = k + 1;
            }
        }
    }
    int arr[4] = {9,2,6,9};
    while (1)
    {
        total = 0;
        isok = false;
        cout<<"Please Input 4 numbers: "<<endl;
        for (int i = 0;i < 4;++i)
        {
            cin>>arr[i];
        }
        if (arr[0] == 0||arr[1] == 0)
        {
            break;
        }
        dfs(0,arr,4);
        for (int n = 0;n <total;++n)
        {
            for (int t = 0;t < fh_num;++t)
            {
                for (int c = 0;c < 5;++c)
                {
                    sum = matht(ar[n],fh[t],c);
                    if (abs(sum - 24.0) < 1E-6)
                    {
                        isok = true;
                        switch (c)
                        {
                        case 0:
                            sprintf(str,"((%d%c%d)%c%d)%c%d = 24",ar[n][0],toCh(fh[t][0]),ar[n][1],toCh(fh[t][1]),ar[n][2],toCh(fh[t][2]),ar[n][3]);
                            break;
                        case 1:
                            sprintf(str,"(%d%c(%d%c%d))%c%d = 24",ar[n][0],toCh(fh[t][1]),ar[n][1],toCh(fh[t][0]),ar[n][2],toCh(fh[t][2]),ar[n][3]);
                            break;
                        case 2:
                            sprintf(str,"(%d%c%d)%c(%d%c%d) = 24",ar[n][0],toCh(fh[t][0]),ar[n][1],toCh(fh[t][2]),ar[n][2],toCh(fh[t][1]),ar[n][3]);
                            break;
                        case 3:
                            sprintf(str,"%d%c((%d%c%d)%c%d) = 24",ar[n][0],toCh(fh[t][2]),ar[n][1],toCh(fh[t][0]),ar[n][2],toCh(fh[t][1]),ar[n][3]);
                            break;
                        case 4:
                            sprintf(str,"%d%c(%d%c(%d%c%d)) = 24",ar[n][0],toCh(fh[t][2]),ar[n][1],toCh(fh[t][1]),ar[n][2],toCh(fh[t][0]),ar[n][3]);
                            break;
                        }
                        goto L;
                    }
                }
            }
        }
    L:
        printf("%s\n",&str);
        if (isok)
        {
            cout<<"YES!"<<endl;
        }
        else
            cout<<"No!"<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/itachi7/p/2625685.html