#include <stdio.h>
#include <math.h>
int p24(float*,int,int,int,int,int,int,int);//定义函数
void main(void)
{ FILE *file;
float a[4];
char b[4]={'+','-','*','/'};
int count=0,result;
int i,j,k,l,m1,m2,m3;
file=fopen("d:\\1.out","w");//使得数据输出在D 盘
printf(" Now please input four number!\n");
printf("One:");
scanf("%f",&a[0]);
printf("Two:");
scanf("%f",&a[1]);
printf("Three:");
scanf("%f",&a[2]);
printf("Four:");
scanf("%f",&a[3]);//输入数据
for(i=0;i<4;i++)
{ for(j=0;j<4;j++)
{ if(j==i)
continue;
for(k=0;k<4;k++)
{ if(k==i||k==j)
continue;
for(l=0;l<4;l++)
{if(l==i||l==j||l==k)//实现i,j,k,l,几个数不相同
continue;
for(m1=0;m1<4;m1++)
{ for(m2=0;m2<4;m2++)
{for(m3=0;m3<4;m3++)
{ if((result=p24(a,i,j,k,l,m1,m2,m3))!=0)//判定p4函数的结果并与下面的switch语句结合最终输出所有的结果是24的组合
{ switch(result)
{ case 1:
fprintf(file,"Solution %d:((%2.0f%c%2.0f)%c%2.0f)%c%2.0f\n",count,a[i],b[m1] ,a[j],b[m2],a[k],b[m3],a[l]); break;
case2: fprintf(file,"Solution%d:(%2.0f%c%2.0f)%c(%2.0f%c%2.0f)\n",count,a[i],b[m1],a[j],b[m2],a[k],b[m3],a[l]);break;
case 3:
fprintf(file,"Solution%d:(%2.0f%c(%2.0f%c%2.0f))%c%2.0f\n",count,a[i],b[m1]
,a[j],b[m2],a[k],b[m3],a[l]);break;
case 4:
fprintf(file,"Solution %d:%2.0f%c((%2.0f%c%2.0f)%c%2.0f)\n",count,a[i],b[m1]
,a[j],b[m2],a[k],b[m3],a[l]);break;
case 5:
fprintf(file,"Solution%d:%2.0f%c(%2.0f%c(%2.0f%c%2.0f))\n",count,a[i],b[m1]
,a[j],b[m2],a[k],b[m3],a[l]);break;
default:break;
}
count++;//记录所有的符合要求的组合数
}}}}}}}}
if(count==0)
printf("No solution!\n");}
int p24(float *a,int i,int j,int k,int l,int m1,int m2,int m3)//实现几个数据的+-*/的运算符号的结合得出结果进行判定
{
char b[4]={'+','-','*','/'};
float y=0,y1,y2;
switch(b[m1])
{case '+':y=a[i]+a[j];break;
case '-':y=a[i]-a[j];break;
case '*':y=a[i]/a[j];break;
}
switch(b[m2])
{case '+':y=y+a[k];break;
case '-':y=y-a[k];break;
case '*':y=y*a[k];break;
case '/':y=y/a[k];break;
}
switch(b[m3])
{case '+':y=y+a[l];break;
case '-':y=y-a[l];break;
case '*':y=y*a[l];break;
case '/':y=y/a[l];break;
}
if(fabs(y-24.0)<0.1)
return(1);
y=0;y1=0;
switch(b[m1])
{case '+':y=a[i]+a[j];break;
case '-':y=a[i]-a[j];break;
case '*':y=a[i]*a[j];break;
case '/':y=a[i]/a[j];break;
}
switch(b[m3])
{case '+':y1=a[k]+a[l];break;
case '-':y1=a[k]-a[l];break;
case '*':y1=a[k]*a[l];break;
case '/':y1=a[k]/a[l];break;
}
switch(b[m2])
{ case '+':y=y+y1;break;
case '-':y=y-y1;break;
case '*':y=y*y1;break;
case '/':if(y1==0.0) y1=0.001;
y=y/y1;break;
}
if(fabs(y-24.0)<0.1)
return(2);
y=0;y1=0;
switch(b[m2])
{
case '+':y1=a[j]+a[k];break;
case '-':y1=a[j]-a[k];break;
case '*':y1=a[j]*a[k];break;
case '/':y1=a[j]/a[k];break;
}
switch(b[m1])
{
case '+':y=a[i]+y1;break;
case '-':y=a[i]-y1;break;
case '*':y=a[i]*y1;break;
case '/':if(y1==0.0) y1=0.001;y=a[i]/y1;break;
}
switch(b[m3])
{ case '+':y=y+a[l];break;
case '-':y=y-a[l];break;
case '*':y=y*a[l];break;
case '/':y=y/a[l];break;
}
if(fabs(y-24.0)<0.1)
return(3);
y=0;y1=0;
switch(b[m2])
{case '+':y1=a[j]+a[k];break;
case '-':y1=a[j]-a[k];break;
case '*':y1=a[j]*a[k];break;
case '/':y1=a[j]/a[k];break;
}
switch(b[m3])
{case '+':y1=y1+a[l];break;
case '-':y1=y1-a[l];break;
case '*':y1=y1*a[l];break;
case '/':y1=y1/a[l];break;
}
switch(b[m1])
{case '+':y=a[i]+y1;break;
case '-':y=a[i]-y1;break;
case '*':y=a[i]*y1;break;
case '/':if(y1==0.0) y1=0.001;y=a[i]/y1;break;
}
if(fabs(y-24.0)<0.1)
return(4);
y=0;y1=0;y2=0;
switch(b[m3])
{case '+':y1=a[k]+a[l];break;
case '-':y1=a[k]-a[l];break;
case '*':y1=a[k]*a[l];break;
case '/':y1=a[k]/a[l];break;
}
switch(b[m2])
{ case '+':y2=a[j]+y1;break;
case '-':y2=a[j]-y1;break;
case '*':y2=a[j]*y1;break;
case '/':if(y1==0.0) y1=0.001;y2=a[j]/y1;break;
}
switch(b[m1])
{
case '+':y=a[i]+y2;break;
case '-':y=a[i]-y2;break;
case '*':y=a[i]*y2;break;
case '/':if(y2==0.0) y2=0.001;
y=a[i]/y2;break;
}
if(fabs(y-24.0)<0.1)
return(5);
return(0);}