-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfraction_string.cc
More file actions
76 lines (63 loc) · 1.63 KB
/
Copy pathfraction_string.cc
File metadata and controls
76 lines (63 loc) · 1.63 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
#include "common/common.h"
std::string fraction_lessone(int n, int d);
std::string fraction(int n, int d) {
assert(d != 0);
std::string res;
if (n >= d) {
std::ostringstream oss;
oss<<(n / d);
res += oss.str();
res.push_back('.');
} else {
res += "0.";
}
res += fraction_lessone(n % d, d);
return res;
}
struct vector_asmap {
std::vector<int> pos;
vector_asmap(int m) : pos(m + 1, -1) { }
bool contains(int c) {
return pos[c] != -1;
}
int& operator[](int c) {
return pos[c];
}
};
std::string fraction_lessone(int n, int d) {
assert(n < d);
assert(d != 0);
int r = n;
vector_asmap reminder_pos_map(d + 1);
std::vector<char> t;
int pos = 0;
while (r != 0 && !reminder_pos_map.contains(r)) {
std::cout<<"reminder: "<<r<<" pos: "<<pos<<std::endl;
char q = (10 * r / d) + '0';
t.push_back(q);
reminder_pos_map[r] = pos;
// next reminder.
r = (10 * r) % d;
++pos;
}
if (r == 0) {
// no reminder left.
return std::string(t.begin(), t.end());
} else {
int rep_pos = reminder_pos_map[r];
std::vector<char>::iterator i = t.begin();
std::advance(i, rep_pos);
std::string res(t.begin(), i);
res.push_back('[');
res += std::string(i, t.end());
res.push_back(']');
return res;
}
}
void test() {
std::string res = fraction(2, 3);
std::cout<<res<<std::endl;
}
int main(int argc, char* argv[]) {
std::cout<<fraction(atoi(argv[1]), atoi(argv[2]))<<std::endl;
}