-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23sumOJ.cpp
83 lines (76 loc) · 1.81 KB
/
23sumOJ.cpp
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
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int o(int a, int b, int v)
{
if (v == 0)
{
return a + b;
}
else if (v == 1)
{
return a - b;
}
else if (v == 2)
{
return a * b;
}
}
int main()
{
while (true)
{
vector<int> a(5);
int total = 0;
for (int i = 0; i <= 4; ++i)
{
cin >> a[i];
total += a[i];
}
if (total == 0)
{
break;
}
sort(a.begin(), a.end());
bool ok = false;
do
{
for (int p1 = 0; p1 <= 2 && ok == false; ++p1)
{
for (int p2 = 0; p2 <= 2 && ok == false; ++p2)
{
for (int p3 = 0; p3 <= 2 && ok == false; ++p3)
{
for (int p4 = 0; p4 <= 2 && ok == false; ++p4)
{
long ans1 = o(a[0],a[1], p1);
ans1 = o(ans1,a[2], p2);
ans1 = o(ans1,a[3], p3);
ans1 = o(ans1,a[4], p4);
if (ans1 == 23)
{
ok = true;
break;
}
}
}
}
}
} while (next_permutation(a.begin(), a.end()) && ok == false);
if (ok == true)
{
cout << "Possible\n";
}
else
{
cout << "Impossible\n";
}
}
return 0;
}