-
Notifications
You must be signed in to change notification settings - Fork 2
/
rationalratio.cpp
43 lines (39 loc) · 1.04 KB
/
rationalratio.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
#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <ctype.h>
#include <map>
typedef long long ll;
using namespace std;
int main()
{
double decimal;
ll decimalLoc;
ll nonRepeat;
ll n , d;
string decimalString;
string::iterator sItr;
ll repeating;
cin >> decimalString;
cin >> repeating;
sItr = find(decimalString.begin(), decimalString.end(), '.');
decimalLoc = sItr - decimalString.begin();
ll intPart;
intPart = stoll(decimalString.substr(0, decimalLoc));
nonRepeat = decimalString.size() - decimalLoc - repeating - 1;
d = pow(10, decimalString.size() - decimalLoc - 1) - pow(10, nonRepeat);
n = stoll(decimalString.substr(decimalLoc + 1, (repeating + nonRepeat)));
if (nonRepeat != 0)
{
n -= stoll(decimalString.substr(decimalLoc + 1, nonRepeat));
}
n += (d*intPart);
ll g = __gcd(n, d);
cout << n/g << "/" << d/g << "\n";
return 0;
}