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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int counter=0,total=0;
double  eps = 0.0000001;

map,int> table;
class Solution {
public:
    bool solution(double a,double b)
    {
        return fabs(a+b-24)& 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& nums) {
        sort(nums.begin(),nums.end());
        do{
            if(solution(nums))
            {
                for(int i=0;i 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 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 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 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 = "<

你可能感兴趣的:(C++列举所有的24点组合(无递归))