1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
| #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int num[7],a[6],Operator[4];
char ope[5]={'0','+','-','*','/'};
const int Plus=1,Minus=2,Mul=3,Div=4;
bool solved=false;
inline int compute(int x,int y,int op)
{
if(op==Plus)
{
return x+y;
}
if(op==Minus)
{
return x-y;
}
if(op==Mul)
{
return x*y;
}
if(op==Div)
{
return x/y;
}
}
void print()
{
solved=true;
for(int i=1;i<=3;++i)
{
if(num[2*i]>num[2*i-1])
{
int t=num[2*i];
num[2*i]=num[2*i-1];
num[2*i-1]=t;
}
printf("%d%c%d=%d\n",num[2*i-1],ope[Operator[i]],num[2*i],compute(num[2*i-1],num[2*i],Operator[i]));
}
printf("\n");
}
bool operation(int m,int op,int dep)
{
int n=m+1;
int ans;
num[2*dep-1]=a[m];
num[2*dep]=a[n];
Operator[dep]=op;
ans=compute(a[m],a[n],op);
//if(ans<=0)return false;
a[m]=abs(ans);
for(int i=n;i<=4-(dep-1);++i)
{
a[i]=a[i+1];
}
return true;
}
void search(int k)
{
if(k==4)
{
if(a[1]==24)
{
print();
exit(0);
}
else return;
}
for(int i=1;i<=4-(k-1)-1;++i)
{
for(int o=1;o<=4;++o)
{
if(o!=4||(a[i+1]!=0&&a[i]%a[i+1]==0))
{
int numb[7],ab[7],ob[4];
memset(numb,0,sizeof(numb));
memset(ab,0,sizeof(ab));
memset(ob,0,sizeof(ob));
memcpy(ab,a,sizeof(a));
memcpy(numb,num,sizeof(num));
memcpy(ob,Operator,sizeof(Operator));
operation(i,o,k);
//if(operation(i,o,k))
search(k+1);
memcpy(a,ab,sizeof(a));
memcpy(num,numb,sizeof(num));
memcpy(Operator,ob,sizeof(Operator));
}
}
}
}
void inPut()
{
for(int i=1;i<=4;++i)
{
scanf("%d",&a[i]);
}
}
int main()
{
//freopen("luogu1236.in","r",stdin);
inPut();
while(!solved)
{
search(1);
if(!solved)
next_permutation(a+1,a+1+4);
}
if(!solved)
{
printf("No answer!\n");
}
return 0;
}
|