-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (80 loc) · 1.5 KB
/
Copy pathmain.cpp
File metadata and controls
98 lines (80 loc) · 1.5 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
// https://terms.naver.com/entry.nhn?cid=58944&docId=3574654&categoryId=58970
#include <bits/stdc++.h>
using namespace std;
int T, p, q;
int posA, posB;
int cycleStartPos;
int nineMK;
int getGCD(int a, int b) {
if (b == 0)
return a;
return getGCD(b, a%b);
}
bool isNotINF() {
int q2 = q;
posA = posB = 0;
while (q2 % 2 == 0) {
q2 /= 2;
posA++;
}
while (q2 % 5 == 0) {
q2 /= 5;
posB++;
}
cycleStartPos = max(posA, posB);
nineMK = q2;
if (q2 == 1)
return true;
else
return false;
}
int getDist() {
// make (10^N)-1 sample : 9, 99, 999, 19, 29, 2999, 29999
int temp = 9 % nineMK;
int ret = 1;
while (temp != 0) {
ret++;
temp = temp * 10 + 9;
temp = temp % nineMK;
}
return ret;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> T;
for (int tc = 1; tc <= T; tc++) {
cin >> p >> q;
int d = getGCD(p, q);
p /= d;
q /= d;
string ans = to_string(p / q);
if (p % q == 0 || isNotINF()) {
// 유한소수 표현 가능
p = p % q * 10;
if (p != 0)
ans += '.';
while (p != 0) {
ans += p / q + '0';
p = p % q * 10;
}
}
else {
// 무한소수 (또는 순환하는 무한소수)
int cycleDist = getDist();
ans += '.';
for (int i = 0; i < cycleStartPos; i++) {
p = p % q * 10;
ans += p / q + '0';
}
ans += '(';
p = p % q * 10;
for (int i = 0; i < cycleDist; i++) {
ans += p / q + '0';
p = p % q * 10;
}
ans += ')';
}
cout << "#" << tc << " " << ans << "\n";
}
return 0;
}