-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path10344.cpp
35 lines (29 loc) · 873 Bytes
/
10344.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
/*
brute force > recursive backtracking
difficulty: easy
date: 19/Mar/2020
problem: check if some arithmetic expression of 5 given numbers will result in 23
hint: check all combination of operators for each permutation of the given numbers
by: @brpapa
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int a[5];
bool bt(int acc, int i) {
// valor total acc, operando-o com a[i]
if (i == 5) return acc == 23;
return bt(acc+a[i], i+1) || bt(acc-a[i], i+1) || bt(acc*a[i], i+1);
}
int main() {
while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] && (a[0] || a[1] || a[2] || a[3] || a[4])) {
bool ok = false;
sort(a, a+5);
do {
if (ok = (ok || bt(a[0], 1))) break;
} while (next_permutation(a, a+5));
cout << (ok? "Possible" : "Impossible") << endl;
}
return 0;
}