探究24点牌有多少种可能

游戏的规则是:你会被分配抽取N张扑克牌,分别从A-K,其中我们规定牌面为A的牌,其数值为1点;牌面为J、Q、K的牌,其数值分别为11、12、13点;数字牌的点数与其所表示的数字一致。现在你可以通过移动这N张牌的任意次序,并在两两之间插入四种运算符号(注:除法在这里仅考虑整除的情况,例如3/2=1,4/2=2)。经过上述操作后,我们按照运算顺序计算表达式的结果,如果结果恰好凑成24点,则游戏取得胜利。解决如下问题:

当你读入N张牌和其对应的数值,你有多少种方法能够恰好凑出24点,并且要输出每种方法的具体情况。

我们规定:两种方法不同,当且仅当对应的两个表达式字符串不同。

例如:给出的5张牌为1 2 3 3 5其中有两个数值3的牌,但在实际测试数据中不会有任何区分。此时,方案和方案是完全相同的解,在结果中只需要输出一次。

输入包括两行,第一行一个整数N,代表卡牌数量,保证N<=6

第二行输入N个整数,依次表示卡牌的数值。

输出每种可能的方案,按照字符串格式输出,计算数与符号之间不需要加空格。

最后一行输出一个整数,表示可行的解的数量。

样例输入
4
10 2 13 6
样例输出
6+10/2+13
6+13+10/2
10/2+6+13
10/2+13+6
13+6+10/2
13+10/2+6
6

嗯,这是一篇理解较为透彻的实例介绍【关于24点游戏的编程思路与基本算法】,它这里从根本上讲解编程过程,和引用到的算法,期望对你学习和编写程序助力,链接:https://blog.csdn.net/wangqiulin123456/article/details/8145545?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-14-8145545-blog-54709207.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-14-8145545-blog-54709207.pc_relevant_default&utm_relevant_index=21

#include <bits/stdc++.h> 
using namespace std; 
const double PRECISION = 1E-6; 
const int COUNT_OF_NUMBER  = 4; 
const int NUMBER_TO_BE_CAL = 24; 
double number[COUNT_OF_NUMBER]; 
string expression[COUNT_OF_NUMBER]; 
bool Search(int n) 
{ 
    if (n == 1) { 
        if ( fabs(number[0] - NUMBER_TO_BE_CAL) < PRECISION ) { 
            cout << expression[0] << endl; 
            return true; 
        } else { 
            return false; 
        } 
    } 
    for (int i = 0; i < n; i++) { 
        for (int j = i + 1; j < n; j++) { 
            double a, b; 
            string expa, expb; 
            a = number[i]; 
            b = number[j]; 
            number[j] = number[n - 1]; 
            expa = expression[i]; 
            expb = expression[j]; 
            expression[j] = expression[n - 1]; 
            expression[i] = '(' + expa + '+' + expb + ')'; 
            number[i] = a + b; 
            if ( Search(n - 1) ) return true; 
            
            expression[i] = '(' + expa + '-' + expb + ')'; 
            number[i] = a - b; 
            if ( Search(n - 1) ) return true; 
            
            expression[i] = '(' + expb + '-' + expa + ')'; 
            number[i] = b - a; 
            if ( Search(n - 1) ) return true; 
                        
            expression[i] = '(' + expa + '*' + expb + ')'; 
            number[i] = a * b; 
            if ( Search(n - 1) ) return true; 
            if (b != 0) { 
                expression[i] = '(' + expa + '/' + expb + ')'; 
                number[i] = a / b; 
                if ( Search(n - 1) ) return true; 
            }  
            if (a != 0) { 
                expression[i] = '(' + expb + '/' + expa + ')'; 
                number[i] = b / a; 
                if ( Search(n - 1) ) return true; 
            } 
            number[i] = a; 
            number[j] = b; 
            expression[i] = expa; 
            expression[j] = expb; 
        } 
    } 
    return false; 
} 
void main() 
{ 
    for (int i = 0; i < COUNT_OF_NUMBER; i++) { 
        char buffer[20]; 
        int  x; 
        cin >> x; 
        number[i] = x; 
        itoa(x, buffer, 10); 
        expression[i] = buffer; 
    } 
    if ( Search(COUNT_OF_NUMBER) ) { 
        cout << "Success." << endl; 
    } else { 
        cout << "Fail." << endl; 
    }         
}

如有帮助,望采纳

//注意要在输出结果后面加\n,不然会有格式错误,坑爹啊!!!
#include "stdio.h"
#include "stdlib.h"
 
char op[5]={'#','+','-','*','/',};
 
float cal(float x,float y,int op)
{
 switch(op)
 {
 case 1:return x+y;
 case 2:return x-y;
 case 3: return x*y;
 case 4: return x/y;
 }
}
 
float calculate_model1(float i,float j,float k,float t,int op1,int op2,int op3)
{
 float r1,r2,r3;
 r1 = cal(i,j,op1);
 r2 = cal(r1,k,op2);
 r3 = cal(r2,t,op3);
 return r3;
}
 
float calculate_model2(float i,float j,float k,float t,int op1,int op2,int op3)
{
 float r1,r2,r3;
 r1 = cal(j,k,op2);
 r2 = cal(i,r1,op1);
 r3 = cal(r2,t,op3);
 return r3;
}
 
float calculate_model3(float i,float j,float k,float t,int op1,int op2,int op3)
{
 float r1,r2,r3 ;
 r1 = cal(k,t,op3);
 r2 = cal(j,r1,op2);
 r3 = cal(i,r2,op1);
 return r3;
}
 
 
float calculate_model4(float i,float j,float k,float t,int op1,int op2,int op3)
{
 float r1,r2,r3;
 r1 = cal(j,k,op2);
 r2 = cal(r1,t,op3);
 r3 = cal(i,r2,op1);
 return r3;
}
 
float calculate_model5(float i,float j,float k,float t,int op1,int op2,int op3)
{
 float r1,r2,r3 ;
 r1 = cal(i,j,op1);
 r2 = cal(k,t,op3);
 r3 = cal(r1,r2,op2);
 return r3;
}
 
int get24(int i,int j,int k,int t)
{
 int op1,op2,op3;
 int flag=0;
 for(op1=1;op1<=4;op1++)
 for(op2=1;op2<=4;op2++)
  for(op3=1;op3<=4;op3++)
  {
  if(calculate_model1(i,j,k,t,op1,op2,op3)==24){
 printf("((%d%c%d)%c%d)%c%d\n",i,op[op1],j,op[op2],k,op[op3],t);flag = 1;goto OUT;
  }
  if(calculate_model2(i,j,k,t,op1,op2,op3)==24){
 printf("(%d%c(%d%c%d))%c%d\n",i,op[op1],j,op[op2],k,op[op3],t);flag = 1;goto OUT;
 }
  if(calculate_model3(i,j,k,t,op1,op2,op3)==24){
 printf("%d%c(%d%c(%d%c%d))\n",i,op[op1],j,op[op2],k,op[op3],t);flag = 1;goto OUT;
 }
  if(calculate_model4(i,j,k,t,op1,op2,op3)==24){
 printf("%d%c((%d%c%d)%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t);flag = 1;goto OUT;
 }
  if(calculate_model5(i,j,k,t,op1,op2,op3)==24){
 printf("(%d%c%d)%c(%d%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t);flag = 1;goto OUT;
 }
  }
 
OUT: return flag;
}
 
int main()
{
 int x,y,m,n;
 int i,j,k,t;
 int in[4];
 int flag;
 for(i=0;i<4;i++)
 scanf("%d",&in[i]);
 for(i=0;i<4;i++){
 for(j=0;j<4;j++){
  if(j==i) continue;
  for(k=0;k<4;k++){
  if(i==k||j==k) continue;
  for(t=0;t<4;t++){
   if(t==i||t==j||t==k) continue;
   x = in[i];
   y = in[j];
   m = in[k];
   n = in[t];
   flag = get24(x,y,m,n);
   if(flag ==1) goto END;
  }
  }
 }
 }
 if(flag == 0)
 printf("-1\n");
 
 END: // system("pause");
 
 return 0;
}