参考代码如下:
#include<敏谨iostream>
#include<math.h>
using namespace std
const double MIN=1E-6
void Print(int *Rank,double *FourNum)
{
for(int i=0i<笑拿前4i++)
cout<<FourNum[Rank[i]]<<" "
cout<<endl
}
void Calculate_24(int *Rank,int *FourNum,char *Oper,int i,int j,int k,bool &def)
{
double res=0
switch(i)
{
case 0:
res=FourNum[Rank[0]]+FourNum[Rank[1]]
break
case 1:
res=FourNum[Rank[0]]-FourNum[Rank[1]]
break
case 2:
res=FourNum[Rank[0]]*FourNum[Rank[1]]
break
case 3:
res=FourNum[Rank[0]]/FourNum[Rank[1]]
break
}
switch(j)
{
case 0:
res=res+FourNum[Rank[2]]
break
case 1:
res=res-FourNum[Rank[2]]
break
case 2:
res=res*FourNum[Rank[2]]
break
case 3:
res=res/FourNum[Rank[2]]
break
}
switch(k)
{
case 0:
res=res+FourNum[Rank[3]]
break
case 1:
res=res-FourNum[Rank[3]]
break
case 2:
res=res*FourNum[Rank[3]]
break
case 3:
res=res/FourNum[Rank[3]]
break
}
if(fabs(res-24)>MIN)
return
else
{
def=true
for(int num=1num<=7num++)
{
switch(num)
{
case 1:
cout<<FourNum[Rank[0]]
break
case 3:
cout<<FourNum[Rank[1]]
break
case 5:
cout<<FourNum[Rank[2]]
break
case 7:
cout<<FourNum[Rank[3]]
break
case 2:
cout<<Oper[i]
break
case 4:
cout<<Oper[j]
break
case 6:
cout<<Oper[k]
break
}
}
cout<<endl
}
}
void SearchTree(int Depth,int *Rank,int *FourNum,char *Oper,bool &def)
{
int i,j,k
if(Depth==4)
{
for(i=0i<4i++)
for(j=0j<4j++)
for(k=0k<4k++)
Calculate_24(Rank,FourNum,Oper,i,j,k,def)
}
else
{
for(i=0i<4i++)
{
int Remember=0
for(j=0j<Depthj++)
{
if(Rank[j]==i)
Remember=1
}
if(Remember)
continue
Rank[Depth]=i
SearchTree(Depth+1,Rank,FourNum,Oper,def)
}
}
}
int main()
{
int a[4],b[4],time
char c[4]={'+','-','*','/'}
bool def=false
cin>>time
while(time--)
{
for(int i=0i<4i++)//输入碰清测试数据
cin>>a[i]
cout<<"所有可能的结果:"<<endl
SearchTree(0,b,a,c,def)
if(def==false)
cout<<"No"<<endl
}
return 0
}
#include<stdio.h>伏液double fun(double a1,double a2,int b)
{switch(b)
{case 0:return (a1+a2)
case 1:return (a1-a2)
case 2:return (a1*a2)
case 3:return (a1/a2)
}
}
void main()
{int i,j,k,l,n,m,r,save[4]
double num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111
char sign[5]="+-*/"
printf("input 4 numbers:")
for(i=0i<4i++)
{scanf("%lf",num+i)save[i]=num[i]}
for(i=0i<4i++)
for(j=0j<4j++)
if(j!=i)
{for(k=0k<4k++)
if(k!=i&&k!=j)
{for(l=0l<4l++)
if(l!=i&&l!=j&&l!=k)
{for(n=0n<4n++)
for(m=0m<4m++)
for(r=0r<4r++)
{tem1=fun(num[i],num[j],n)
tem2=fun(tem1,num[k],m)
tem3=fun(tem2,num[l],r)
if(tem3==24.0)printf("枝厅没{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l])
else if(tem3==-24.0)printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l])
else if(tem3==1.0/24.0)printf("%d%c{(%d%c%d)%c%d}=24\猛纳n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k])
else if(tem3==-1.0/24.0)printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j])
else
{tem1=fun(num[i],num[j],n)
tem2=fun(num[k],num[l],r)
tem3=fun(tem1,tem2,m)
if(tem3==24.0) printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l])
}
}
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)