我是一一举例的
#include<iostream>
using namespace std;
void swap(float *a,float *b,float *c,float *d,int x)
{ float n,m,p,q;
n=*a;m=*b;p=*c;q=*d;
switch(x)
{
case 1:{*a=n;*b=m;*c=p;*d=q;};break;
case 2:{*a=n;*b=m;*d=p;*c=q;};break;
case 3:{*a=n;*c=m;*b=p;*d=q;};break;
case 4:{*a=n;*c=m;*d=p;*b=q;};break;
case 5:{*a=n;*d=m;*b=p;*c=q;};break;
case 6:{*a=n;*d=m;*c=p;*b=q;};break;
case 7:{*b=n;*a=m;*c=p;*d=q;};break;
case 8:{*b=n;*a=m;*d=p;*c=q;};break;
case 9:{*b=n;*c=m;*a=p;*d=q;};break;
case 10:{*b=n;*c=m;*d=p;*a=q;};break;
case 11:{*b=n;*d=m;*a=p;*c=q;};break;
case 12:{*b=n;*d=m;*c=p;*a=q;};break;
case 13:{*c=n;*a=m;*b=p;*d=q;};break;
case 14:{*c=n;*a=m;*d=p;*b=q;};break;
case 15:{*c=n;*d=m;*a=p;*b=q;};break;
case 16:{*c=n;*d=m;*b=p;*a=q;};break;
case 17:{*c=n;*b=m;*a=p;*d=q;};break;
case 18:{*c=n;*b=m;*d=p;*a=q;};break;
case 19:{*d=n;*a=m;*b=p;*c=q;};break;
case 20:{*d=n;*a=m;*c=p;*b=q;};break;
case 21:{*d=n;*b=m;*a=p;*c=q;};break;
case 22:{*d=n;*b=m;*c=p;*a=q;};break;
case 23:{*d=n;*c=m;*a=p;*b=q;};break;
case 24:{*d=n;*c=m;*b=p;*a=q;};break;
}
}
char fh(int i)
{ char r;
if(i==1) r=‘+‘;
else if(i==2) r=‘-‘;
else if(i==3) r=‘*‘;
else r=‘/‘;
return r;
}
float suan(int i,float a,float b)
{
if(i==1) return a+b;
else if(i==2) return a-b;
else if(i==3) return a*b;
else if(i==4) return a/b;
}
int qiude(float a,float b,float c,float d)
{int i1,i2,i3,n;
float x,y,p,q,s=0,s1=0;
x=a;y=b;p=c;q=d;
for(n=1;n<=24;n++)
{
swap(&a,&b,&c,&d,n);
for(i1=1;i1<=4;i1++)
for(i2=1;i2<=4;i2++)
for(i3=1;i3<=4;i3++)
{
s=suan(i1,a,b);
s=suan(i2,s,c);
s=suan(i3,s,d);
if(s-24<0.001&&s-24>-0.001)
{
cout<<"(("<<a<<fh(i1)<<b<<")"<<fh(i2)<<c<<")"<<fh(i3)<<d<<"=24"<<endl;
return 0;
}
else
s=0;
s=suan(i1,a,b);
s=suan(i2,c,s);
s=suan(i3,s,d);
if(s-24<0.001&&s-24>-0.001)
{
cout<<"("<<c<<fh(i2)<<"("<<a<<fh(i1)<<b<<"))"<<fh(i3)<<d<<"=24"<<endl;
return 0;
}
else
s=0;
s=suan(i1,a,b);
s=suan(i2,s,c);
s=suan(i3,d,s);
if(s-24<0.001&&s-24>-0.001)
{
cout<<d<<fh(i3)<<"(("<<a<<fh(i1)<<b<<")"<<fh(i2)<<c<<")=24"<<endl;
return 0;
}
else
s=0;
s=suan(i1,a,b);
s=suan(i2,c,s);
s=suan(i3,d,s);
if(s-24<0.001&&s-24>-0.001)
{
cout<<d<<fh(i3)<<"("<<c<<fh(i2)<<"("<<a<<fh(i1)<<b<<"))=24"<<endl;
return 0;
}
else
s=0;
}
for(i1=1;i1<=4;i1++)
for(i2=1;i2<=4;i2++)
for(i3=1;i3<=4;i3++)
{
s=suan(i1,a,b);
s1=suan(i3,c,d);
s=suan(i2,s,s1);
if(s-24<0.001&&s-24>-0.001)
{
cout<<"("<<a<<fh(i1)<<b<<")"<<fh(i2)<<"("<<c<<fh(i3)<<d<<")"<<"=24"<<endl;
return 0;
}
else
s=0;
}
a=x;b=y;c=p;d=q;
}
cout<<"无法算得24点。"<<endl;
return 0;}
int main()
{ float a=0,b=0,c=0,d=0;
cout<<"要算24点吗?四个数之间以空格分开,要结束程序输-1."<<endl;
while(a!=-1)
{
cin>>a;
if(a==-1)
break;
cin>>b>>c>>d;
qiude(a,b,c,d);
cout<<endl;
}
cout<<"Over.\n";
return 0;
}