#include<cstdio>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
int a[4],cnt;
char b[4]={'*','+','-','/'};
void calc(double &a,double b,double c){
if(c==0)a*=b;
else if(c==1)a+=b;
else if(c==2)a-=b;
else if(b>0)a=a*1.0/b;
else a=-1;
}
bool calc1(double a1,double a2,double a3,double a4,int b1,int b2,int b3,double p){
int t1=a1,t2=a2,t3=a3,t4=a4;
if(p==1){//(((A?B)?C)?D)
calc(a1,a2,b1);if(a1==-1)return 0;
calc(a1,a3,b2);if(a1==-1)return 0;
calc(a1,a4,b3);
if(fabs(a1-24)<1e-8){
printf("(((%d%c%d)%c%d)%c%d)\n",t1,b[b1],t2,b[b2],t3,b[b3],t4);
return 1;
}
}else if(p==3){//((A?(B?C))?D)
calc(a2,a3,b2);if(a2==-1)return 0;
calc(a1,a2,b1);if(a1==-1)return 0;
calc(a1,a4,b3);
if(fabs(a1-24)<1e-8){
printf("((%d%c(%d%c%d))%c%d)\n",t1,b[b1],t2,b[b2],t3,b[b3],t4);
return 1;
}
}else if(p==2){//((A?B)?(C?D))
calc(a1,a2,b1);if(a1==-1)return 0;
calc(a3,a4,b3);if(a3==-1)return 0;
calc(a1,a3,b2);
if(fabs(a1-24)<1e-8){
printf("((%d%c%d)%c(%d%c%d))\n",t1,b[b1],t2,b[b2],t3,b[b3],t4);
return 1;
}
}else if(p==4){//(A?((B?C)?D))
calc(a2,a3,b2);if(a2==-1)return 0;
calc(a2,a4,b3);if(a2==-1)return 0;
calc(a1,a2,b1);
if(fabs(a1-24)<1e-8){
printf("(%d%c((%d%c%d)%c%d))\n",t1,b[b1],t2,b[b2],t3,b[b3],t4);
return 1;
}
}else {//(A?(B?(C?D)))
calc(a3,a4,b3);if(a3==-1)return 0;
calc(a2,a3,b2);if(a2==-1)return 0;
calc(a1,a2,b1);
if(fabs(a1-24)<1e-8){
printf("(%d%c(%d%c(%d%c%d)))\n",t1,b[b1],t2,b[b2],t3,b[b3],t4);
return 1;
}
}return 0;
}
int main(){
scanf("%d %d %d %d",&a[0],&a[1],&a[2],&a[3]);
int p,a1,a2,a3,a4,b1,b2,b3;
bool mark[5]={0};
sort(a,a+4);
for(p=1;p<=5;p++)
for(a1=0;a1<4;a1++){
mark[a1]=1;
for(b1=0;b1<4;b1++)
for(a2=0;a2<4;a2++){
if(mark[a2])continue;
mark[a2]=1;
for(b2=0;b2<4;b2++)
for(a3=0;a3<4;a3++){
if(mark[a3])continue;
for(b3=0;b3<4;b3++){
if(calc1(a[a1],a[a2],a[a3],a[6-a1-a2-a3],b1,b2,b3,p))return 0;
}
}mark[a2]=0;
}mark[a1]=0;
}
return 0;
}
3-速算24点
最新推荐文章于 2024-11-09 19:34:49 发布