-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartition_string.cpp
139 lines (118 loc) · 2.84 KB
/
partition_string.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
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
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <sstream>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::istringstream;
using std::istream_iterator;
using std::ofstream;
string file_name {"magic_number.txt"};
void write_title() {
ofstream ofs;
ofs.open(file_name, ofstream::app);
ofs << "# The Generated Magic Number for the Reference of True Fans!" << endl;
ofs.close();
}
void write_to_file(string s, int result) {
ofstream ofs;
ofs.open(file_name, ofstream::app);
ofs << "#### " ;
for (auto c : s) {
if (c == '+' || c == '-' || c == '*' || c == '/')
ofs << " " << c << " ";
else
ofs << c;
}
ofs << " = " << result << endl;
ofs.close();
}
// Split the input string using space as the delimiter.
vector<string> split(string s) {
istringstream iss(s);
vector<string> result(istream_iterator<string> {iss}, istream_iterator<string>());
return result;
}
// Insert operators to the generated string
// step starts from 1
// cr: current result
// hor: high order result
// pn: previous number
void calc(vector<string> vs, string & s, int step, int cr, int hor, int pn) {
if (step == static_cast<int>(vs.size())) {
cout << s << "=" << cr << "+" << hor << "=" << cr + hor << endl;
write_to_file(s, cr + hor);
return;
}
int n = stoi(vs[step]);
// The length of the number:
int nl = static_cast<int> (vs[step].size());
// add '+'
s += "+";
s += vs[step];
calc(vs, s, step + 1, cr + n + hor, 0, n);
s = s.substr(0, s.size() - 1 - nl);
// add '-'
s += "-";
s += vs[step];
calc(vs, s, step + 1, cr - n + hor, 0, -1 * n);
s = s.substr(0, s.size() - 1 - nl);
// add '*'
s += "*";
s += vs[step];
calc(vs, s, step + 1, cr - pn, hor == 0 ? pn * n : hor * n, 0);
s = s.substr(0, s.size() - 1 - nl);
// add '/'
if (n != 0) {
s += "/";
s += vs[step];
calc(vs, s, step + 1, cr - pn, hor == 0 ? pn / n : hor / n, 0);
s = s.substr(0, s.size() - 1 - nl);
}
}
void do_calc(string s) {
vector<string> result(split(s));
string fns{result[0]}; // first number in string
int fn = stoi(result[0]); // first number
calc(result, fns, 1, fn, 0, fn);
}
//Do the partition of the string.
void dfs(string & s, int step) {
if (step == static_cast<int>(s.size()) - 1) {
cout << s << endl;
do_calc(s);
return;
}
dfs(s, step + 1);
s = s.substr(0, step + 1) + " " + s.substr(step + 1);
dfs(s, step + 2);
s = s.substr(0, step + 1) + s.substr(step + 2);
}
void test_partition() {
/*
string s0 {"a"};
string s1 {"abcd"};
string s2 {"abcde"};
string s3 {"abcdef"};
cout << "------" << endl;
dfs(s0, 0);
cout << "------" << endl;
dfs(s1, 0);
cout << "------" << endl;
dfs(s2, 0);
cout << "------" << endl;
dfs(s3, 0);
*/
string s {"19260817"};
cout << "------" << endl;
dfs(s, 0);
}
int main() {
write_title();
test_partition();
return 0;
}