C++列举所有的24点组合(无递归)

事实上只有495种可能的输入,这其中有404种能计算出24点.
所有可能的输入:
total = ABCD+AAAA+AAAB+AABB+AABC
total = C(9,4)+C(9,1)+A(9,2)+C(9,2)+(987)/2
算出total =504;

穷举所有的total组合并判断是否能算出24点:
#include <iostream>
#include <unistd.h>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int counter=0,total=0;
double  eps = 0.0000001;

map<vector<int>,int> table;
class Solution {
public:
    bool solution(double a,double b)
    {
        return fabs(a+b-24)<eps||fabs(a-b-24)<eps||fabs(a*b-24)<eps||b&&fabs(a/b-24)<eps;
    }
    bool solution(double a,double b,double c)
    {
        bool f1 = solution(a+b,c)||solution(a-b,c)||solution(a*b,c)||b&&solution(a/b,c);
        bool f2 = solution(a,b+c)||solution(a,b-c)||solution(a,b*c)||c&&solution(a,b/c);
        return f1||f2;
    }
    bool solution(vector<int>& nums)
    {
        double a = nums[0],b = nums[1],c = nums[2],d = nums[3];
        bool f1 = solution(a+b,c,d)||solution(a-b,c,d)||solution(a*b,c,d)||solution(a/b,c,d);
        bool f2 = solution(a,b+c,d)||solution(a,b-c,d)||solution(a,b*c,d)||solution(a,b/c,d);
        bool f3 = solution(a,b,c+d)||solution(a,b,c-d)||solution(a,b,c*d)||solution(a,b,c/d);
        return f1||f2||f3;
    }
    bool judgePoint24(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        do{
            if(solution(nums))
            {
                for(int i=0;i<nums.size();i++)
                {
                    cout<<nums[i]<<" ";
                }
                cout<<endl;
                counter++;
                return true;
            }
        }while(next_permutation(nums.begin(),nums.end()));

        return false;
    }
};

int main()
{
    Solution s1;
    for(int i=1;i<=9;i++)
    {
        for(int j=i+1;j<=9;j++)
        {
            for(int k=j+1;k<=9;k++)
            {
                for(int p=k+1;p<=9;p++)
                {
                    vector<int> nums(4);
                    nums[0]=i;nums[1]=j;nums[2]=k;nums[3]=p;
                    s1.judgePoint24(nums);
                    total++;
                }
            }
        }
    }
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=9;j++)
        {

            for(int k=j+1;k<=9;k++)
            {

                vector<int> nums(4);
                if(j!=i&&k!=i&&k!=j)
                {

                    nums[0]=i;nums[1]=i;nums[2]=j;nums[3]=k;
                    s1.judgePoint24(nums);
                    total++;
                }


            }
        }
    }
    for(int i=1;i<=9;i++)
    {
        vector<int> nums(4);
        nums[0]=i;nums[1]=i;nums[2]=i;nums[3]=i;
        s1.judgePoint24(nums);
        total++;
        for(int j=i+1;j<=9;j++)
        {

                nums[0]=i;nums[1]=i;nums[2]=i;nums[3]=j;
                s1.judgePoint24(nums);
                total++;

                nums[0]=i;nums[1]=j;nums[2]=j;nums[3]=j;
                s1.judgePoint24(nums);
                total++;
        }
    }
    for(int i=1;i<=9;i++)
    {
        vector<int> nums(4);
        for(int j=i+1;j<=9;j++)
        {

                nums[0]=i;nums[1]=i;nums[2]=j;nums[3]=j;
                s1.judgePoint24(nums);
                total++;
        }
    }



    cout<<"counter = "<<counter<<endl;
    cout<<"total = "<<total<<endl;

}

我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan

    原文作者:小太阳花儿
    原文地址: https://www.jianshu.com/p/61851723361e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞