YukicoderContest289 D問題P2 「Number Conversion」
https://gyazo.com/83df7a2ec990e4b4f64f261c77fc730d
問題概要
制約
$ X \leq 10^5
解法・お気持ち
浮動小数点だと面倒なので,整数になおします.
最初与えられる$ Xは$ X / 1と表すことができます.
よって,$ Xが整数になるまで分子と分母に10をかけていけばいいです.
あとは分子と分母の最大公約数を計算して,既約分数にすればよいです.
実装がうまくできませんでした.(汚い)
計算量
$ O(1)
新たな学び
反省点
コード
code: cpp
int main() {
string X;
cin >> X;
if (count(X.begin(), X.end(), '.') == 0) {
cout << X << "/" << 1 << endl;
return 0;
}
LL t = -1;
LL bottom = 1;
REP(i, t) bottom *= 10;
X.erase(X.find('.') + X.begin());
LL up = stoll(X);
LL g = gcd(bottom, up);
bottom /= g;
up /= g;
cout << up << "/" << bottom << endl;
}