`
shaojwa
  • 浏览: 4536 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

24点游戏的一个小代码

阅读更多
这段代码帮助你判断四个0-9 之间的数能不能通过加减乘除得到24点,要是可以,程序也会显示一种可行的计算方法,这其实是一个枚举,效率不高。
#include <stdio.h>
double four[4], three[3];
char steps[3][100];

int judge2(double x, double y)
{
	if (24 == x + y)
	{
		sprintf(steps[2],"%lf + %lf  = %lf", x, y, x + y);
		return 24;
	}
	if (24 == x - y)
	{
		sprintf(steps[2],"%lf - %lf  = %lf", x, y, x - y);
		return 24;
	}
	if (24 == y - x)
	{
		sprintf(steps[2],"%lf - %lf  = %lf", y, x, y - x);
		return 24;
	}
	if (24 == x * y)
	{
		sprintf(steps[2],"%lf * %lf  = %lf", x, y, x * y);
		return 24;
	}
	if (x != 0)
	{
		if ( 24 == y / x )
		{
			sprintf(steps[2],"%lf / %lf  = %lf", y, x, y / x);
			return 24;
		}
	}
	if (y != 0)
	{
		if ( 24 == x / y )
		{
			sprintf(steps[2],"%lf / %lf  = %lf", x, y, x / y);
			return 24;
		}
	}
	return 0;
}

int judge3(double three[])
{
	int i, j;
	for (i  = 0; i < 2; i++)
		for(j = i + 1; j < 3; j++)
		{
			if(24 == judge2(three[3-i-j],three[i] - three[j]))
			{
				sprintf(steps[1],"%lf - %lf  = %lf",three[i],three[j],three[i] - three[j]);
				return 24;
			}

			if(24 == judge2(three[3-i-j],three[j] - three[i]))
			{
				sprintf(steps[1],"%lf - %lf  = %lf",three[j],three[i],three[j] - three[i]);
				return 24;
			}

			if(24 == judge2(three[3-i-j],three[i] + three[j]))
			{
				sprintf(steps[1],"%lf + %lf  = %lf",three[i],three[j],three[i] + three[j]);
				return 24;
			}

			if(24 == judge2(three[3-i-j],three[i] * three[j]))
			{
				sprintf(steps[1],"%lf * %lf  = %lf",three[i],three[j],three[i] * three[j]);
				return 24;
			}

			if (three[i] != 0) 
			{
				if(24 == judge2(three[3-i-j],three[j] / three[i]))
				{
					sprintf(steps[1],"%lf / %lf  = %lf",three[j],three[i],three[j] / three[i]);
					return 24;
				}
			}
			if(three[j] != 0)
			{
				if(24 == judge2(three[3-i-j],three[i] / three[j]))
				{
					sprintf(steps[1],"%lf / %lf  = %lf",three[i],three[j],three[i] / three[j]);
					return 24;
				}
			}			
		}
		return 0;
}

int judge4(double four[])
{
	int i,j,k,n;
	for (i = 0; i < 3; i++)
		for ( j = i + 1; j < 4; j++)
		{
			n = 0;
			for (k = 0; k < 4; k++)
				if (k != i && k != j)
					three[n++] = four[k];

			three[2] = four[i] + four[j];
			if(24 == judge3(three))
			{
				sprintf(steps[0],"%lf + %lf = %lf", four[i],four[j],three[2]);
				return 24;
			}

			three[2] = four[i] - four[j];
			if (24 == judge3(three))
			{
				sprintf(steps[0],"%lf - %lf = %lf", four[i],four[j],three[2]);
				return 24;
			}

			three[2] = four[j] - four[i];
			if (24 == judge3(three))
			{
				sprintf(steps[0],"%lf - %lf = %lf", four[j],four[i],three[2]);
				return 24;
			}

			three[2] = four[j] * four[i];
			if (24 == judge3(three))
			{
				sprintf(steps[0],"%lf * %lf = %lf", four[i],four[j],three[2]);
				return 24;
			}

			if (four[i] != 0)
			{
				three[2] = four[j] / four[i];
				if (24 == judge3(three))
				{
					sprintf(steps[0],"%lf / %lf = %lf", four[j],four[i],three[2]);
					return 24;
				}
			}

			if (four[j] != 0)
			{
				three[2] = four[i] / four[j];
				if (24 == judge3(three))
				{
					sprintf(steps[0],"%lf / %lf = %lf", four[i],four[j],three[2]);
					return 24;
				}
			}
		}
		return 0;
}

int main()
{
	int i, flag;
	while(printf("Please input 4 integers between 0-9:")
		&& scanf("%lf %lf %lf %lf", &four[0], &four[1], &four[2], &four[3]))
	{
		flag = 0;	
		for(i = 0; i < 4; i++)
		{
			if(four[i] > 9 || four[i] < 0) {
				printf("Error!Not all of numbers between 0-9!");
				flag = 1;
				break;
			}
		}
		if(1 == flag) 
		{
			continue;
		}
		else 
		{
			if (24 == judge4(four))
			{
				printf("Yes! the four numbers can get 24 points in the following steps:\n");
				for(i = 0; i < 3; i++)
				{
					printf("%s\n",steps[i]);
				}
			}
			else 
			{
				printf("Sorry! the four numbers can not get 24 points anyway!\n");
			}
		}		
	}
	return  0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics