-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_bin.cpp
More file actions
129 lines (115 loc) · 2.99 KB
/
Copy pathtrain_bin.cpp
File metadata and controls
129 lines (115 loc) · 2.99 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
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
124
125
126
127
128
129
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <list>
#include <set>
#include <cstring>
#include <stack>
#include <cassert>
#include <cmath>
using namespace std;
#define TARGET 10
#define EPSILON 0.000001
class Binary {
public:
virtual bool works(float a, float b) = 0;
virtual float doIt(float a, float b) = 0;
};
class Plus : public Binary {
public:
bool works(float a, float b) { return true; }
float doIt(float a, float b) { return a + b; }
};
class Minus : public Binary {
public:
bool works(float a, float b) { return true; }
float doIt(float a, float b) { return a - b; }
};
class Times : public Binary {
public:
bool works(float a, float b) { return true; }
float doIt(float a, float b) { return a * b; }
};
class Divide : public Binary {
public:
bool works(float a, float b) { return abs(b) > EPSILON; }
float doIt(float a, float b) { return a/b; }
};
class Power : public Binary {
public:
bool works(float a, float b) { return true; }
float doIt(float a, float b) { return pow(a, b); }
};
class Reverse : public Binary {
private:
Binary *original;
public:
Reverse(Binary *o) : original(o) {}
bool works(float a, float b) { return original->works(b, a); }
float doIt(float a, float b) { return original->doIt(b, a); }
};
list<Binary*> binaries;
void init(bool exp) {
binaries.push_back(new Plus());
binaries.push_back(new Minus());
binaries.push_back(new Reverse(new Minus()));
binaries.push_back(new Times());
binaries.push_back(new Divide());
binaries.push_back(new Reverse(new Divide()));
if (exp) {
binaries.push_back(new Power());
binaries.push_back(new Reverse(new Power()));
}
}
bool check(list<float>::iterator begin, list<float>::iterator end) {
if (next(begin) == end) {
return *begin == TARGET;
}
for (list<float>::iterator i = begin; i != end; ++i) {
for (list<float>::iterator j = next(i); j != end; ++j) {
for(list<Binary*>::iterator o = binaries.begin(); o != binaries.end(); ++o) {
if (!(*o)->works(*i, *j)) {
continue;
}
float res = (*o)->doIt(*i, *j);
float oldi = *i;
float oldj = *j;
*i = *begin;
*j = res;
if (check(next(begin), end)) {
return true;
}
*i = oldi;
*j = oldj;
}
}
}
return false;
}
int main(int argc, char ** argv) {
if (argc < 2) {
cout << "Usage: foo number [-e]" << endl;
return 1;
}
bool exp = false;
if (argc > 2) {
if (strcmp(argv[2], "-e") == 0) {
exp = true;
} else {
cout << "Unrecognised switch: " << argv[2] << endl;
return 1;
}
}
init(exp);
list<float> digs;
for (unsigned int i = 0; i < strlen(argv[1]); ++i) {
int dig = argv[1][i] - '0';
if (dig < 0 || dig >= 10) {
cout << "Digits must all be digits" << endl;
return 1;
}
digs.push_back(argv[1][i] - '0');
}
cout << (check(digs.begin(), digs.end()) ? "1" : "0") << endl;
return 0;
}