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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值