这个是我以前编的24点,虽然没完全符合要求,但勉强可以看一下.
我在这个程序并没有把结果完全输出来,不过您改一下应该还是比较简单的,
至于如果要用综合式子把结果打印出来的话,还要加入一些包装,您也自己写吧.
#include<stdio.h>
#include<math.h>
int flag=0;
void calculate(float a,float b,float *p)
{
*(p++)=a+b;
*(p++)=a-b;
*(p++)=b-a;
*(p++)=a*b;
if(b!=0)
*(p++)=a/b;
else *(p++)=20027;
if(a!=0)
*(p)=b/a;
else *p=20027;
}
void function (float a,float b,float c,float d)
{
float a1[6]={0},a2[6]={0},result[6]={0};
int i,j,k;
calculate(a,b,a1);
calculate(c,d,a2);
for(i=0;i<6;i++)
for(j=0;j<6;j++)
{
calculate(a1[i],a2[j],result);
for(k=0;k<6;k++)
if(fabs((double)24-result[k])<1e-3)
{
switch(i)
{
case 0:printf("%.0f+%.0f=%.0f\n",a,b,a1[0]);break;
case 1:printf("%.0f-%.0f=%.0f\n",a,b,a1[1]);break;
case 2:printf("%.0f-%.0f=%.0f\n",b,a,a1[2]);break;
case 3:printf("%.0f*%.0f=%.0f\n",a,b,a1[3]);break;
case 4:printf("%.0f/%.0f=%g\n",a,b,a1[4]);break;
case 5:printf("%.0f/%.0f=%g\n",b,a,a1[5]);break;
}
switch(j)
{
case 0:printf("%.0f+%.0f=%.0f\n",c,d,a2[0]);break;
case 1:printf("%.0f-%.0f=%.0f\n",c,d,a2[1]);break;
case 2:printf("%.0f-%.0f=%.0f\n",d,c,a2[2]);break;
case 3:printf("%.0f*%.0f=%.0f\n",c,d,a2[3]);break;
case 4:printf("%.0f/%.0f=%g\n",c,d,a2[4]);break;
case 5:printf("%.0f/%.0f=%g\n",d,c,a2[5]);break;
}
switch(k)
{
case 0:printf("%g+%g=%g\n",a1[i],a2[j],result[0]);break;
case 1:printf("%g-%g=%g\n",a1[i],a2[j],result[1]);break;
case 2:printf("%g-%g=%g\n",a2[j],a1[i],result[2]);break;
case 3:printf("%g*%g=%g\n",a1[i],a2[j],result[3]);break;
case 4:printf("%g/%g=%g\n",a1[i],a2[j],result[4]);break;
case 5:printf("%g/%g=%g\n",a2[j],a1[i],result[5]);break;
}
flag=1;/*标记已计算出来*/
i=6;
j=6;
break;
}
}
if(flag==0)
{
for(i=0;i<6;i++)
{
calculate(a1[i],c,a2);
for(j=0;j<6;j++)
{
calculate(a2[j],d,result);
for(k=0;k<6;k++)
if(fabs(24-result[k])<0.001)
{
switch(i)
{
case 0:printf("%.0f+%.0f=%.0f\n",a,b,a1[0]);break;
case 1:printf("%.0f-%.0f=%.0f\n",a,b,a1[1]);break;
case 2:printf("%.0f-%.0f=%.0f\n",b,a,a1[2]);break;
case 3:printf("%.0f*%.0f=%.0f\n",a,b,a1[3]);break;
case 4:printf("%.0f/%.0f=%g\n",a,b,a1[4]);break;
case 5:printf("%.0f/%.0f=%g\n",b,a,a1[5]);break;
}
switch(j)
{
case 0:printf("%g+%.0f=%g\n",a1[i],c,a2[0]);break;
case 1:printf("%g-%.0f=%g\n",a1[i],c,a2[1]);break;
case 2:printf("%.0f-%g=%g\n",c,a1[i],a2[2]);break;
case 3:printf("%g*%.0f=%g\n",a1[i],c,a2[3]);break;
case 4:printf("%g/%.0f=%g\n",a1[i],c,a2[4]);break;
case 5:printf("%.0f/%g=%g\n",c,a1[i],a2[5]);break;
}
switch(k)
{
case 0:printf("%g+%.0f=%g\n",a2[j],d,result[0]);break;
case 1:printf("%g-%.0f=%g\n",a2[j],d,result[1]);break;
case 2:printf("%.0f-%g=%g\n",d,a2[j],result[2]);break;
case 3:printf("%g*%.0f=%g\n",a2[j],d,result[3]);break;
case 4:printf("%g/%.0f=%g\n",a2[j],d,result[4]);break;
case 5:printf("%.0f/%g=%g\n",d,a2[j],result[5]);break;
}
flag=1;/*标记已计算出来*/
i=6;
j=6;
break;
}
}
}
if(flag==0)
{
for(i=0;i<6;i++)
{
calculate(a1[i],d,a2);
for(j=0;j<6;j++)
{
calculate(a2[j],c,result);
for(k=0;k<6;k++)
if(fabs(24-result[k])<0.001)
{
switch(i)
{
case 0:printf("%.0f+%.0f=%.0f\n",a,b,a1[0]);break;
case 1:printf("%.0f-%.0f=%.0f\n",a,b,a1[1]);break;
case 2:printf("%.0f-%.0f=%.0f\n",b,a,a1[2]);break;
case 3:printf("%.0f*%.0f=%.0f\n",a,b,a1[3]);break;
case 4:printf("%.0f/%.0f=%g\n",a,b,a1[4]);break;
case 5:printf("%.0f/%.0f=%g\n",b,a,a1[5]);break;
}
switch(j)
{
case 0:printf("%g+%.0f=%g\n",a1[i],d,a2[0]);break;
case 1:printf("%g-%.0f=%g\n",a1[i],d,a2[1]);break;
case 2:printf("%.0f-%g=%g\n",d,a1[i],a2[2]);break;
case 3:printf("%g*%.0f=%g\n",a1[i],d,a2[3]);break;
case 4:printf("%g/%.0f=%g\n",a1[i],d,a2[4]);break;
case 5:printf("%.0f/%g=%g\n",d,a1[i],a2[5]);break;
}
switch(k)
{
case 0:printf("%g+%.0f=%g\n",a2[j],c,result[0]);break;
case 1:printf("%g-%.0f=%g\n",a2[j],c,result[1]);break;
case 2:printf("%.0f-%g=%g\n",c,a2[j],result[2]);break;
case 3:printf("%g*%.0f=%g\n",a2[j],c,result[3]);break;
case 4:printf("%g/%.0f=%g\n",a2[j],c,result[4]);break;
case 5:printf("%.0f/%g=%g\n",c,a2[j],result[5]);break;
}
flag=1;
i=6;
j=6;
break;
}
}
}
}
}
}
main()
{
float a,b,c,d;
printf("Please input four number between 1-12:\n");
scanf("%f%f%f%f",&a,&b,&c,&d);
function(a,b,c,d);
if(!flag)
{
function(b,c,a,d);
if(!flag)
{
function(c,d,a,b);
if(!flag)
{
function(a,d,b,c);
if(!flag)
{
function(a,c,b,d);
if(!flag)
function(b,d,a,c);
}
}
}
}
if(!flag)
printf("Sorry,the four number can't be calculated out!");
getch();
}