面:24点游戏

输入4个数,求解得到24点,并输出相应的表达式。
使用面向对象开发,只能用整形,比如输入6 6 6 6 ,则输出6+6+6+6=24

 

测试的时候我会用3 3 8 8,也可能会有5 5 5 1 来测试

3 3 8 8
(((3+8)/3)*8) = 24

 

3,3,8,8算24点的算法有:

1,8/(3-8/3)=24

2,8/(3-(8/3))=24

 

8÷(3-8÷3)=8÷1/3=24

 

 

 

5 5 5 1
((5-(1/5))*5) = 24

 

 

 

加减乘除24点(难度系数:2颗星)

计算24点有什么窍门或技巧吗?

用穷举法列出24点的解

 

24点游戏算法(C语言实现)

"速算24"算法思路

 

关于24点游戏的编程思路与基本算法

https://blog.csdn.net/r1254/article/details/46914431 48行计算24点C语言代码

https://blog.csdn.net/tigerisland45/article/details/81905291 dfs

 

 

 

4X6=24

3X8=24

 

a  _   b  _   c   _   d:

 

C41 * C41 *C41 * A44这就是错的原因,没有考虑全部的排列方式?

 

 

最新版本,

#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;
#define OP(o)   do { a[1].n = a[0].n o a[1].n; \
	sprintf(a[1].s, "(%s"#o"%s)", a[0].s, strcpy(t.s, a[1].s)); \
	if (dfs(n - 1, a + 1, r)) return 1; \
	a[1] = temp; } while (0)


typedef struct data {
	double n;//存中间结果
	char s[8 * 7];//存表达式 
} data;
typedef int Ptype;//para type

data num[4], t;

class Solution{
public:
	Solution();
	~Solution();
	int dfs(int n, data a[], int r);//TODO
};


Solution::Solution(){
	//sth
}

Solution::~Solution(){
	//sth
}

int 
Solution::dfs( int n, data a[], int r ) 
{// n,数字个数; a[] 存输入的n个数,r 要得到的结果
	int i, j;
	if ( n == 1 )
		if ( a[0].n-r > 1e-8 || a[0].n-r < -1e-8 ) return 0;// !=0
		else return 1;
	for ( i = 0; i < n; ++i ) {
		swap(a[i], a[0] );
		for ( j = 1; j < n; ++j ) {
			swap(a[j], a[1] );//指定为data类型
			data temp = a[1];
			OP( + );
			OP( - );
			OP( * );
			if ( a[1].n > 1e-8 || a[1].n < -1e-8 )
				OP( / );
			swap(a[j], a[1] );
		}
		swap(a[i], a[0] );
	}
	return 0;
}


int main()
{

	int r = 24, i;
	printf("输入4个数字,以空格隔开:\n");
	for ( i = 0; i < 4; ++i ) {
		scanf( "%s", num[i].s );
		sscanf( num[i].s, "%lf", &num[i].n );
	}

	Solution* ans=new Solution;
	if (ans->dfs( 4, num, r ) )
		printf( "%s = %d\n", num[3].s, r );
	else
		printf( "不存在相应的24点的组合\n" );
	return 0;
}

 

混合:可正常测试出3 3 8 8和 5 5 5 1了。

又被说,,,

#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;
#define SWAP(tp, a, b) do {tp v=a; a=b; b=v;} while (0)
#define OP(o)   do { a[1].n = a[0].n o a[1].n; \
	sprintf(a[1].s, "(%s"#o"%s)", a[0].s, strcpy(t.s, a[1].s)); \
	if (dfs(n - 1, a + 1, r)) return 1; \
	a[1] = temp; } while (0)


typedef struct data {
	double n;//用int存的话会得不出正常答案
	char s[8 * 7];//存表达式 
} data;
typedef int Ptype;//para type

data num[4], t;

class Solution{
public:
	Solution();
	~Solution();
	int dfs(int n, data a[], int r);//TODO

	int TotalCount(Ptype a, Ptype b, Ptype c, Ptype d);//C4 1 
private:
	void Calculate(int fl,Ptype n,Ptype &sum, char &s);//A3 3
	int Is24(Ptype a, Ptype b, Ptype c, Ptype d);
	int Count(Ptype a, Ptype b, Ptype c, Ptype d);
protected:
	//data num[4], t;
};


Solution::Solution(){
	//sth
}

Solution::~Solution(){
	//sth
}

void
Solution::Calculate(int fl,Ptype n,Ptype &sum, char &s)
{ //+-*/   1234
	switch (fl)
	{//flag
	case 1:
		sum += n;
		s='+';
		break;
	case 2:
		sum -= n;
		s='-';
		break;
	case 3:
		sum *= n;
		s='*';
		break;
	case 4:	
		{
			if (sum%n) sum = 9999;//指向一个得不到24的值
			sum /= n;//else
		}
		break;
	default:
		break;
	}
}

int 
Solution::Is24(Ptype a, Ptype b, Ptype c, Ptype d)
{ //a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
	Ptype sum;
	char s1,s2,s3;// 存运算符
	for (int j = 1; j <= 4; j++)
	{//第1个位置的符号
		for (int k = 1; k <= 4; k++)
		{//第2个位置
			for (int l = 1; l <= 4; l++)
			{//第3个位置
				sum = 0;
				sum += a;
				Calculate(j,b,sum,s1);
				Calculate(k,c,sum,s2);
				Calculate(l,d,sum,s3);
				if (sum == 24)
				{//格式化输出
					printf("组合:((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					return 1;
				}
			}
		}
	}
	return 0;
}

int 
Solution::Count(Ptype a, Ptype b, Ptype c, Ptype d)
{//A3 3
	return Is24(a, b, c, d) + Is24(a, b, d, c) + Is24(a, c, b, d) + Is24(a, c, d, b) + Is24(a, d, b, c) + Is24(a, d, c, b);
}

int 
Solution::TotalCount(Ptype a, Ptype b, Ptype c, Ptype d)
{//C4 1
	return Count(a, b, c, d) + Count(b, a, c, d) + Count(c, a, b, d) + Count(d, a, b, c);
}

int 
Solution::dfs( int n, data a[], int r ) 
{// n,数字个数; a[] 存输入的n个数,r 要得到的结果
	int i, j;
	if ( n == 1 )
		if ( a[0].n-r > 1e-8 || a[0].n-r < -1e-8 )
			return 0;
		else return 1;
	for ( i = 0; i < n; ++i ) {
		SWAP( data, a[i], a[0] );
		for ( j = 1; j < n; ++j ) {
			SWAP( data, a[j], a[1] );//指定为data类型
			data temp = a[1];
			OP( + );
			OP( - );
			OP( * );
			if ( a[1].n > 1e-8 || a[1].n < -1e-8 )
				OP( / );
			SWAP( data, a[j], a[1] );
		}
		SWAP( data, a[i], a[0] );
	}
	return 0;
}


int main()
{
	/*
	 *    int a, b, c, d;
	 *    cout<<"请输入4个数,以空格隔开:"<<endl;
	 *    cin >> a >> b >> c >> d;
	 *
	 *    Solution* ans=new Solution;
	 *    int ct = ans->TotalCount(a, b, c, d);//A4 4
	 *    delete ans;
	 *
	 *    if (ct == 0) cout << "不存在相应的24点的组合" << endl;
	 *    cout<<"总数:"<<ct<<endl;
	 *    return 0;
	 */

	int r = 24, i;
	printf("输入4个数字,以空格隔开:\n");
	for ( i = 0; i < 4; ++i ) {
		scanf( "%s", num[i].s );
		sscanf( num[i].s, "%lf", &num[i].n );
	}

	Solution* ans=new Solution;
	if (ans->dfs( 4, num, r ) )
		printf( "%s = %d\n", num[3].s, r );
	else
		printf( "不存在相应的24点的组合\n" );
	return 0;
}

 

 

 

被大佬嫌弃,,,, 可哥,不想改了

 

#include <iostream>
#include <stdio.h>

typedef int Ptype;//para type

using namespace std;

class Solution{
public:
	Solution();
	~Solution();
	int TotalCount(Ptype a, Ptype b, Ptype c, Ptype d);//C4 1 
private:
	void Calculate(int i,Ptype n,Ptype &sum, char &s);//A3 3
	int Is24(Ptype a, Ptype b, Ptype c, Ptype d);
	int Count(Ptype a, Ptype b, Ptype c, Ptype d);
	//int Is24_DFS();//TODO
};


Solution::Solution(){
	//sth
}

Solution::~Solution(){
	//sth
}

void
Solution::Calculate(int i,Ptype n,Ptype &sum, char &s)
{ //+-*/   1234
	switch (i)
	{
	case 1:
		sum += n;
		s='+';
		break;
	case 2:
		sum -= n;
		s='-';
		break;
	case 3:
		sum *= n;
		s='*';
		break;
	case 4:	
		{
			if (sum%n) sum = 9999;//指向一个得不到24的值
			sum /= n;//else
		}
		break;
	default:
		break;
	}
}

int 
Solution::Is24(Ptype a, Ptype b, Ptype c, Ptype d)
{ //a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
	Ptype sum;
	char s1,s2,s3;// 存运算符
	for (int j = 1; j <= 4; j++)
	{//第1个位置的符号
		for (int k = 1; k <= 4; k++)
		{//第2个位置
			for (int l = 1; l <= 4; l++)
			{//第3个位置
				sum = 0;
				sum += a;
				Calculate(j,b,sum,s1);
				Calculate(k,c,sum,s2);
				Calculate(l,d,sum,s3);
				if (sum == 24)
				{//格式化输出
					printf("组合:((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					return 1;
				}
			}
		}
	}
	return 0;
}

int 
Solution::Count(Ptype a, Ptype b, Ptype c, Ptype d)
{//A3 3
	return Is24(a, b, c, d) + Is24(a, b, d, c) + Is24(a, c, b, d) + Is24(a, c, d, b) + Is24(a, d, b, c) + Is24(a, d, c, b);
}

int 
Solution::TotalCount(Ptype a, Ptype b, Ptype c, Ptype d)
{//C4 1
	return Count(a, b, c, d) + Count(b, a, c, d) + Count(c, a, b, d) + Count(d, a, b, c);
}


int main()
{
	int a, b, c, d;
	cout<<"请输入4个数,以空格隔开:"<<endl;
	cin >> a >> b >> c >> d;

	Solution* ans=new Solution;
	int ct = ans->TotalCount(a, b, c, d);//A4 4
	delete ans;

	if (ct == 0) cout << "不存在相应的24点的组合" << endl;
	cout<<"总数:"<<ct<<endl;
	return 0;
}

 

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

class Solution{
public:
	Solution();
	~Solution();
	int TotalCount(int a, int b, int c, int d);//get the total num
private:
	void AddUp(int i,int n,int &sum, char &s);//add nums up
	int Is24(int a, int b, int c, int d);
	int Count(int a, int b, int c, int d);
	//typedef XXX if needed 

};//抽到头文件,大的时候

Solution::Solution(){
	//sth
}

Solution::~Solution(){
	//sth
}
	void
Solution::AddUp(int i,int n,int &sum, char &s)
{//计算
	//+-*/   1234
	switch (i)
	{
	case 1:
		sum += n;
		s='+';
		break;
	case 2:
		sum -= n;
		s='-';
		break;
	case 3:
		sum *= n;
		s='*';
		break;
	case 4:					
		{
			if (sum%n)
			{
				sum = 9999;//指向一个得不到24的值
			} else {
				sum /= n;
			}
		}break;

	default:
		break;
	}
}

	int 
Solution::Is24(int a, int b, int c, int d)
{
	int sum;
	char s1,s2,s3;// 存运算符
	//a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
	for (int j = 1; j <= 4; j++)
	{//第1个位置的符号
		for (int k = 1; k <= 4; k++)
		{//第2个位置的符号
			for (int l = 1; l <= 4; l++)
			{//第3个位置的符号
				sum = 0;
				sum += a;

				AddUp(j,b,sum,s1);
				AddUp(k,c,sum,s2);
				AddUp(l,d,sum,s3);

				if (sum == 24)
				{//判断并格式化输出
					printf("组合:((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					return 1;
				}
			}
		}
	}
	return 0;
}

	int 
Solution::Count(int a, int b, int c, int d)
{
	return Is24(a, b, c, d) + Is24(a, b, d, c) + Is24(a, c, b, d) + Is24(a, c, d, b) + Is24(a, d, b, c) + Is24(a, d, c, b);//各种排列
}

	int 
Solution::TotalCount(int a, int b, int c, int d)
{
	return Count(a, b, c, d) + Count(b, a, c, d) + Count(c, a, b, d) + Count(d, a, b, c);//排列
}




int main()
{
	int a, b, c, d;
	int count = 0;
	cout<<"请输入4个数,以空格隔开:"<<endl;
	cin >> a >> b >> c >> d;

	Solution* ans=new Solution;
	count = ans->TotalCount(a, b, c, d);

	if (count == 0) cout << "不存在相应的24点的组合" << endl;
	cout<<"总数:"<<count<<endl;
	return 0;
}

 

 

修改历史:

我是垃圾,,,写不出来啊,,,抄:

https://blog.csdn.net/huplion/article/details/42522239

#include <iostream>
//#include <cstdio>
//#include <cmath>
#include <stdio.h>
#include <math.h>
using namespace std;
//+-*/   1234
unsigned long long int xx[1001];
int x=1;
int pd;
int f1(int a, int b, int c, int d)
{
	int sum;
	pd=0;
	char s1,s2,s3;
	for (int j = 1; j <= 4; j++)
	{
		for (int k = 1; k <= 4; k++)
		{
			for (int l = 1; l <= 4; l++)
			{
				sum = 0;
				sum += a;
				switch (j)
				{
				case 1:sum += b; break;
				case 2:sum -= b; break;
				case 3:sum *= b; break;
				case 4:
					   {
						   if (sum%b)
						   {
							   sum = 9999;
						   }
						   else
						   {
							   sum /= b;
						   }
					   }break;
				default:
					   break;
				}
				switch (k)
				{
				case 1:sum += c; break;
				case 2:sum -= c; break;
				case 3:sum *= c; break;
				case 4:
					   {
						   if (sum%c)
						   {
							   sum = 9999;
						   }
						   else
						   {
							   sum /= c;
						   }
					   }break;
				default:
					   break;
				}
				switch (l)
				{
				case 1:sum += d; break;
				case 2:sum -= d; break;
				case 3:sum *= d; break;
				case 4:
					   {
						   if (sum%d)
						   {
							   sum = 9999;
						   }
						   else
						   {
							   sum /= d;
						   }
					   }break;
				default:
					   break;
				}
				switch (j)
				{
				case 1:s1='+';break;
				case 2:s1='-';break;
				case 3:s1='*';break;
				case 4:s1='/';break;
				}
				switch (k)
				{
				case 1:s2='+';break;
				case 2:s2='-';break;
				case 3:s2='*';break;
				case 4:s2='/';break;
				}
				switch (l)
				{
				case 1:s3='+';break;
				case 2:s3='-';break;
				case 3:s3='*';break;
				case 4:s3='/';break;
				}
				if (sum == 24)
				{
					if (x==0)
					{
						xx[x]=a*1000000+b*100000+c*10000+d*1000+j*100+k*10+l;
						x++;
						printf("((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					}
					else
					{
						for (int i=1;i<=x;i++)
						{
							if ((a*1000000+b*100000+c*10000+d*1000+j*100+k*10+l)==xx[i])
							{
								pd=1;
								break;
							}
						}
						if (pd==0)
						{
							xx[x]=a*1000000+b*100000+c*10000+d*1000+j*100+k*10+l;
							x++;
							printf("((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
						}
					}
					return 1;
				}
			}
		}
	}
	return 0;
}


int f2(int a, int b, int c, int d)
{
	int sum = 0;
	sum = f1(a, b, c, d) + f1(a, b, d, c) + f1(a, c, b, d) + f1(a, c, d, b) + f1(a, d, b, c) + f1(a, d, c, b);
	
	
	if (sum != 0)
	{
		cout << "存在" << endl;
		return 1;
	}
	else
	{
		cout << "不存在" << endl;
		return 0;
	}
}


int main()
{
	int a, b, c, d;
	int sum;
	cout<<"请输入4个数,以空格隔开如:3 3 8 9"<<endl;
	cin >> a >> b >> c >> d;
	sum = f2(a, b, c, d) + f2(b, a, c, d) + f2(c, a, b, d) + f2(d, a, b, c);
	if (sum == 0)
	{
		cout << "不存在相应的24点的组合" << endl;
	}
	return 0;
}

自己注释:

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
//+-*/   1234
unsigned long long int xx[1001];// 保存值的数组
int x=1;//
int pd;//判断

int f1(int a, int b, int c, int d)
{
	int sum;
	pd=0;
	char s1,s2,s3;// 存运算符
	//a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
	for (int j = 1; j <= 4; j++)
	{//第1个位置的符号
		for (int k = 1; k <= 4; k++)
		{//第2个位置的符号
			for (int l = 1; l <= 4; l++)
			{//第3个位置的符号
				sum = 0;
				sum += a;
				switch (j)
				{
				case 1:sum += b; break;
				case 2:sum -= b; break;
				case 3:sum *= b; break;
				case 4:
					   {
						   if (sum%b)
						   {
							   sum = 9999;
						   }
						   else
						   {
							   sum /= b;
						   }
					   }break;
				default:
					   break;
				}
				switch (k)
				{
				case 1:sum += c; break;
				case 2:sum -= c; break;
				case 3:sum *= c; break;
				case 4:
					   {
						   if (sum%c)
						   {
							   sum = 9999;
						   }
						   else
						   {
							   sum /= c;
						   }
					   }break;
				default:
					   break;
				}
				switch (l)
				{
				case 1:sum += d; break;
				case 2:sum -= d; break;
				case 3:sum *= d; break;
				case 4:
					   {
						   if (sum%d)
						   {//不能被整除
							   sum = 9999;//不能被整除,意味着得不到24
						   }
						   else
						   {//能被整除
							   sum /= d;
						   }
					   }break;
				default:
					   break;
				}

				//存符号
				switch (j)
				{
				case 1:s1='+';break;
				case 2:s1='-';break;
				case 3:s1='*';break;
				case 4:s1='/';break;
				}
				switch (k)
				{
				case 1:s2='+';break;
				case 2:s2='-';break;
				case 3:s2='*';break;
				case 4:s2='/';break;
				}
				switch (l)
				{
				case 1:s3='+';break;
				case 2:s3='-';break;
				case 3:s3='*';break;
				case 4:s3='/';break;
				}

				if (sum == 24)
				{//判断并格式化输出
					if (x==0)
					{//第1个等于24的时候进去?永远不会执行?
						xx[x]=a*1000000+b*100000+c*10000+d*1000+j*100+k*10+l;//存组合及符号
						x++;
						printf("x==0,,,,((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					}
					else
					{
						for (int i=1;i<=x;i++)
						{
							if ((a*1000000+b*100000+c*10000+d*1000+j*100+k*10+l)==xx[i])
							{
								pd=1;
								break;
							}
						}
						if (pd==0)
						{
							xx[x]=a*1000000+b*100000+c*10000+d*1000+j*100+k*10+l;
							x++;
							printf("pd==0,,,((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
						}
					}
					return 1;
				}
			}
		}
	}
	return 0;
}


int f2(int a, int b, int c, int d)
{
	int sum = 0;
	sum = f1(a, b, c, d) + f1(a, b, d, c) + f1(a, c, b, d) + f1(a, c, d, b) + f1(a, d, b, c) + f1(a, d, c, b);//各种排列
	
	
	if (sum != 0)
	{
		//cout << "存在" << endl;
		return 1;
	}
	else
	{
		//cout << "不存在" << endl;
		return 0;
	}
}


int main()
{
	int a, b, c, d;
	int sum;
	cout<<"请输入4个数,以空格隔开:"<<endl;
	cin >> a >> b >> c >> d;
	sum = f2(a, b, c, d) + f2(b, a, c, d) + f2(c, a, b, d) + f2(d, a, b, c);
	if (sum == 0)
	{
		cout << "不存在相应的24点的组合" << endl;
	}
	return 0;
}

 

修改:

看不懂里面写的一些东西是干什么的,,,

不是很懂大佬之前的意思,,

删,

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
//+-*/   1234

void SwitchFunc(int i,int c,int &sum, char &s2)
{

	switch (i)
	{
	case 1:
		sum += c;
		s2='+';
		break;
	case 2:
		sum -= c;
		s2='-';
		break;
	case 3:
		sum *= c;
		s2='*';
		break;
	case 4:
		sum /= c;
		s2='/';
		break;
	default:
		break;
	}
}

int f1(int a, int b, int c, int d)
{
	int sum;
	char s1,s2,s3;// 存运算符
	//a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
	for (int j = 1; j <= 4; j++)
	{//第1个位置的符号
		for (int k = 1; k <= 4; k++)
		{//第2个位置的符号
			for (int l = 1; l <= 4; l++)
			{//第3个位置的符号
				sum = 0;
				sum += a;

				SwitchFunc(j,b,sum,s1);
				SwitchFunc(k,c,sum,s2);
				SwitchFunc(l,d,sum,s3);

				if (sum == 24)
				{//判断并格式化输出
					printf("pd==waimian,,,((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					return 1;
				}
			}
		}
	}
	return 0;
}


int f2(int a, int b, int c, int d)
{
	int sum = 0;
	sum = f1(a, b, c, d) + f1(a, b, d, c) + f1(a, c, b, d) + f1(a, c, d, b) + f1(a, d, b, c) + f1(a, d, c, b);//各种排列

	return sum;
}


int main()
{
	int a, b, c, d;
	int sum;
	cout<<"请输入4个数,以空格隔开:"<<endl;
	cin >> a >> b >> c >> d;
	sum = f2(a, b, c, d) + f2(b, a, c, d) + f2(c, a, b, d) + f2(d, a, b, c);//为啥要这样写???不懂
	if (sum == 0)
	{
		cout << "不存在相应的24点的组合" << endl;
	}
	return 0;
}

 

改,

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
//+-*/   1234

void SwitchFunc(int i,int n,int &sum, char &s)
{

	switch (i)
	{
	case 1:
		sum += n;
		s='+';
		break;
	case 2:
		sum -= n;
		s='-';
		break;
	case 3:
		sum *= n;
		s='*';
		break;
	case 4:
		sum /= n;
		s='/';
		break;
	default:
		break;
	}
}

int Is24(int a, int b, int c, int d)
{
	int sum;
	char s1,s2,s3;// 存运算符
	//a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
	for (int j = 1; j <= 4; j++)
	{//第1个位置的符号
		for (int k = 1; k <= 4; k++)
		{//第2个位置的符号
			for (int l = 1; l <= 4; l++)
			{//第3个位置的符号
				sum = 0;
				sum += a;

				SwitchFunc(j,b,sum,s1);
				SwitchFunc(k,c,sum,s2);
				SwitchFunc(l,d,sum,s3);

				if (sum == 24)
				{//判断并格式化输出
					printf("组合:((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
					return 1;
				}
			}
		}
	}
	return 0;
}


int Count(int a, int b, int c, int d)
{
	return Is24(a, b, c, d) + Is24(a, b, d, c) + Is24(a, c, b, d) + Is24(a, c, d, b) + Is24(a, d, b, c) + Is24(a, d, c, b);//各种排列
}


int main()
{
	int a, b, c, d;
	int count = 0;
	cout<<"请输入4个数,以空格隔开:"<<endl;
	cin >> a >> b >> c >> d;
	count = Count(a, b, c, d) + Count(b, a, c, d) + Count(c, a, b, d) + Count(d, a, b, c);
	if (count == 0) cout << "不存在相应的24点的组合" << endl;
	cout<<"总数:"<<count<<endl;
	return 0;
}

 

对象,

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

class Solution{
public:
	void AddUp(int i,int n,int &sum, char &s)
	{//计算
		//+-*/   1234
		switch (i)
		{
		case 1:
			sum += n;
			s='+';
			break;
		case 2:
			sum -= n;
			s='-';
			break;
		case 3:
			sum *= n;
			s='*';
			break;
		case 4:
			sum /= n;
			s='/';
			break;
		default:
			break;
		}
	}

	int Is24(int a, int b, int c, int d)
	{
		int sum;
		char s1,s2,s3;// 存运算符
		//a b c d 4个数中间分别都有+ - * / 四种运算符的可能性 暴力枚举
		for (int j = 1; j <= 4; j++)
		{//第1个位置的符号
			for (int k = 1; k <= 4; k++)
			{//第2个位置的符号
				for (int l = 1; l <= 4; l++)
				{//第3个位置的符号
					sum = 0;
					sum += a;

					AddUp(j,b,sum,s1);
					AddUp(k,c,sum,s2);
					AddUp(l,d,sum,s3);

					if (sum == 24)
					{//判断并格式化输出
						printf("组合:((%d%c%d)%c%d)%c%d==24\n",a,s1,b,s2,c,s3,d);
						return 1;
					}
				}
			}
		}
		return 0;
	}


	int Count(int a, int b, int c, int d)
	{
		return Is24(a, b, c, d) + Is24(a, b, d, c) + Is24(a, c, b, d) + Is24(a, c, d, b) + Is24(a, d, b, c) + Is24(a, d, c, b);//各种排列
	}

};//class Solution 



int main()
{
	int a, b, c, d;
	int count = 0;
	cout<<"请输入4个数,以空格隔开:"<<endl;
	cin >> a >> b >> c >> d;
	Solution* ans=new Solution;

	count = ans->Count(a, b, c, d) +ans-> Count(b, a, c, d) + ans->Count(c, a, b, d) +ans-> Count(d, a, b, c);//排列
	if (count == 0) cout << "不存在相应的24点的组合" << endl;
	cout<<"总数:"<<count<<endl;
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值