-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcalculate24.cpp
More file actions
81 lines (69 loc) · 1.87 KB
/
calculate24.cpp
File metadata and controls
81 lines (69 loc) · 1.87 KB
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
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
const int INVALID = numeric_limits<int>::min();
bool search(vector<int> nums, int n, int target){
if(n == 1){
if(nums[nums.size()-1] == target){
return true;
}
return false;
}
vector<int> path = nums;
for(int i = 0; i < nums.size(); i++){
if(path[i] == INVALID){
continue;
}
int x = path[i];
path[i] = INVALID;
for(int j = 0; j < nums.size(); j++){
if(path[j] == INVALID){
continue;
}
int y = path[j];
path[j] = INVALID;
int temp = 0;
for(char op = 0; op < 4; op++){
int temp = 0;
if(op == 0){
temp = x + y;
}
else if(op == 1){
temp = x - y;
}
else if(op == 2){
temp = x * y;
}
else if(y != 0){
temp = x / y;
}
path.push_back(temp);
if(search(path,n-1, target)){
return true;
}
path.pop_back();
}
path[j] = y;
}
path[i] = x;;
}
return false;
}
int main(void){
vector<vector<int>> nums = {{11, 11, 5, 1},
{8, 6, 2, 2},
{1000, 100, 10, 1}};
for(int i = 0; i < nums.size(); i++){
for(int j = 0; j < nums[i].size(); j++){
cout<<nums[i][j]<<" ";
}
if(search(nums[i],4, 24)){
cout<<": possible = true" <<endl;
}
else{
cout<<": possible = false" <<endl;
}
}
return 0;
}