-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP202.cpp
61 lines (59 loc) · 1.27 KB
/
P202.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
int len(int a) {
int ret = 1;
while(a > 9) {
++ret;
a/=10;
}
return ret;
}
int main() {
/*
cerr << "Lens: " << endl;
FORI(102)
cerr << i << ": " << len(i) << endl;//*/
int N, D;
while(cin >> N >> D) {
printf("%d/%d = %d.", N, D, N/D);
int printedDigits = len(N/D);
int baseN = N = (N%D);
map<PI,int> m;
//m.insert(make_pair(PI(N,D), 0));
int cycleLength, cycleBegin;
while(true) {
N = N%D;
if(m.find(PI(N,D)) != m.end()) {
//cerr << "Found N=" << N << ", D=" << D << " on position " << m[PI(N,D)] << ", |m|=" << m.size() << endl;
cycleBegin = m[PI(N,D)]-1;
cycleLength = m.size()-cycleBegin;
//if(cycleLength == 1 && N == 0)
//++cycleBegin; // Special case.
break;
}
m[PI(N,D)] = (int)m.size();
N *= 10;
}
// output:
N = 10*baseN;
for(int i = 0; i < cycleBegin; ++i) {
printf("%d", N/D);
N = N%D;
N*=10;
++printedDigits;
}
printf("(");
for(int i = 0; i < cycleLength; ++i) {
printf("%d", N/D);
N = N%D;
N*=10;
++printedDigits;
if(printedDigits == 51) {
printf("...");
break;
}
}
printf(")");
printf("\n");
printf(" %d = number of digits in repeating cycle\n\n", cycleLength);
}
return 0;
}