#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],flg=1
double
num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111
char
sign[5]="+-*/"
printf("输入四个告液数:")
for(i=0i<4i++)
{scanf("%lf",num+i)
save[i]=num[i]if(save[i]>13)flg=0}
if(flg)
{
flg=0
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])return}
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])return}
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])return}
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])return}
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])return}
}
}
}
}
}
}
if(!flg)
printf("NO
ANSWER\n")
}
#include<iostream>using namespace std
struct operation
{
int x
int y
int o
}op[3]
bool co=false
void g(int a[],int b[],int n,int i,int j)
{
int x,y
for(x=0,y=1x<nx++)
{
if(x==i||x==j)continue
b[y++]=a[x]
}
op[4-n].x=a[i]
op[4-n].y=a[j]
}
void f(int a[],int n)
{
int i,j,t,b[4]
if(n==1&&a[0]==24)co=true
for(i=0i<n-1i++)
{
for(j=i+1j<nj++)
{
for(t=1t<=6t++)
{
switch(t)
{
case 1:g(a,b,n,i,j)
b[0]=a[i]+a[j]
op[4-n].o=t
f(b,n-1)
if(co)return
break
case 2:g(a,b,n,i,j)
b[0]=a[i]-a[j]
op[4-n].o=t
f(b,n-1)
if(co)return
break
case 3:g(a,b,n,i,j)
b[0]=a[i]*a[j]
op[4-n].o=t
f(b,n-1)
if(co)return
break
case 4:
if(a[j]!=0&&a[i]%a[j]==0)
{
g(a,b,n,i,j)
b[0]=a[i]/a[j]
op[4-n].o=t
f(b,n-1)
if(co)return
}
break
case 5:g(a,b,n,i,j)
b[0]=a[j]-a[i]
op[4-n].o=t
f(b,n-1)
if(co)return
break
case 6:
if(a[i]!=0&&a[j]%a[i]==0&&a[j]>a[i])
{
g(a,b,n,i,j)
b[0]=a[j]/a[i]
op[4-n].o=t
f(b,n-1)
if(co)return
}
break
}
}
}
}
}
void h(int a[],int n,int x,int y,int h)
{
int i,j,b[4]
bool bx=false,by=false
for(i=0,j=0i<ni++)
{
if(a[i]==x&&bx==false)
{
bx=true
continue
}
if(a[i]==y&&by==false)
{
by=true
continue
}
b[j++]=a[i]
}
for(i=0i<n-1i++)a[i+1]=b[i]
a[0]=h
}
void main()
{
int a[4],i,j
cout<<"输入4个整数:"
for(i=0i<4i++)cin>>a[i]
f(a,4)
if(co)
{
for(i=0i<3i++)
{
for(j=0j<4-ij++)cout<<a[j]<袜闭<" "
cout<<"\n运算 "
switch(op[i].o)
{
case 1:cout<<op[i].x<<"+"仔滚<<op[i].y<<endl
h(a,4-i,op[i].x,op[i].y,op[i].x+op[i].y)
break
case 2:cout<<op[i].x<<"告戚裂-"<<op[i].y<<endl
h(a,4-i,op[i].x,op[i].y,op[i].x-op[i].y)
break
case 3:cout<<op[i].x<<"*"<<op[i].y<<endl
h(a,4-i,op[i].x,op[i].y,op[i].x*op[i].y)
break
case 4:cout<<op[i].x<<"/"<<op[i].y<<endl
h(a,4-i,op[i].x,op[i].y,op[i].x/op[i].y)
break
case 5:cout<<op[i].y<<"-"<<op[i].x<<endl
h(a,4-i,op[i].x,op[i].y,op[i].y-op[i].x)
break
case 6:cout<<op[i].y<<"/"<<op[i].x<<endl
h(a,4-i,op[i].x,op[i].y,op[i].y/op[i].x)
break
}
}
cout<<"24"<<endl
}
else cout<<"不能算出24"<<endl
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)