蒟蒻求助
#include<bits/stdc++.h>
using namespace std;
char c[5]={' ','+','-','*','/'} ;
int a[5];
int f(int x,int y,int k)
{
if(k==1) return x+y;
if(k==2) return abs(x-y);
if(k==3) return x*y;
return(y==0||x<y||x%y!=0) ?-9999999 :x/y;
}
void print(int a,int b,int m,int n,int x,int y,int i,int j,int k)
{
printf("%d%c%d=%d\n",max(a,b),c[i],min(a,b),f(a,b,i));
printf("%d%c%d=%d\n",max(m,n),c[j],min(m,n),f(m,n,j));
printf("%d%c%d=%d\n",max(x,y),c[k],min(x,y),f(x,y,k));
exit(0);
}
int main()
{
for(int i=1;i<=4;i++) scanf("%d",&a[i]);
sort(a+1,a+5);
while(next_permutation(a+1,a+5))
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
for(int k=1;k<=4;k++)
{
if( f ( f ( f ( a[1],a[2],i ) , a[3] , j ) ,a[4] ,k)==24)
{
print(a[1] , a[2] , f(a[1],a[2],i) ,a[3], f(f(a[1],a[2],i),a[3],j), a[4] ,i,j,k);
}
else if( f(f(a[1],a[2],i),f(a[3],a[4],j),k)==24 )
{
print(a[1] ,a[2], a[3] ,a[4] ,f(a[1],a[2],i), f(a[3],a[4],j) ,i,j,k );
}
}
}
}
}
printf("No answer!");
return 0;
}