-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_thought.cpp
68 lines (60 loc) · 1.83 KB
/
4_thought.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
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int eval(int a, int b, char op){
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
}
return -1;
}
int main(){
map<int, string> ans;
map<int, char> m;
m[0] = '*';
m[1] = '/';
m[2] = '+';
m[3] = '-';
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
for(int k = 0; k < 4; ++k){
vector<int> nums = {4,4,4,4};
vector<char> ops = {m[i],m[j],m[k]};
for(int it = 0; it < ops.size(); ++it){
if(ops[it] == '*' || ops[it] == '/'){ //First iterate through operands and execute * and /
nums[it] = eval(nums[it], nums[it+1], ops[it]);
nums.erase(nums.begin()+it+1);
ops.erase(ops.begin()+it);
--it;
}
}
for(int it = 0; it < ops.size(); ++it){ //Then iterate through again and execute remaining operands (+ and -)
nums[it] = eval(nums[it], nums[it+1], ops[it]);
nums.erase(nums.begin()+it+1);
ops.erase(ops.begin()+it);
--it;
}
ans[nums[0]] = "4 " + string(1,m[i]) + " 4 " + string(1,m[j]) + " 4 " + string(1,m[k]) + " 4 = " + to_string(nums[0]); //Answer string
}
}
}
int mm;
cin >> mm;
while(mm--){
int nn;
cin >> nn;
if(ans.find(nn) != ans.end()){
cout << ans[nn] << endl;
} else {
cout << "no solution" << endl;
}
}
return 0;
}