Problem:
実数既約分数化
実数のうち、小数部が循環するものと有限桁のものは分数として表すことができます。
分数で表すことができる実数が与えられたとき、その実数と等しい既約分数（それ以上約分できない分数）を出力するプログラムを作成せよ。
Input
入力は以下の形式で与えられる。
str
１行に、変換したい実数を表す文字列
str
が与えられる。実数の値は 0 より大きい。文字列は数字か「.」、「(」、「)」を含む、長さが 3 以上 8 以下の文字列である。「.」は小数点、「(」は数字の循環の始まり、「)」は数字の循環の終わりを示す。整数部にも小数部にも、必ず１桁以上の数字が与えられるとする。ただし、循環小数が与えられた場合、文字列は以下の条件を満たす。
循環の始まりと終わりのペアは、小数点の右側に一度だけ現れる。
循環の終わりを示す「)」は、文字列の末尾に現れる。
循環の始まりと終わりの間には、必ず１桁以上の数字が与えられる。
Output
実数を既約分数で表した形式（分子の整数に続けて「/」区切りで分母の整数を並べたもの）で出力する。
Sample Input 1
0.(3)
Sample Output 1
1/3
Sample Input 2
1.0
Sample Output 2
1/1
Sample Input 3
5.2(143)
Sample Output 3
52091/9990
Sample Input 4
0.0739
Sample Output 4
739/10000

Submitted solutions:

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(long long i=0;i<n;++i)
#define REPR(i,n) for(long long i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(long long i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define INF 999999999
#define MOD 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v) {for(long long sitr=0;sitr<v.size();++sitr){cin>>v[sitr];}}

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

void kiyaku(int a, int b){
  int n = max(a, b);
  for(int i = 2; i < n; ++i){
    if(a % i == 0 && b % i == 0){
      while(a % i == 0 && b % i == 0){
        a /= i; b /= i;
      }
    }
  }
  cout << a << "/" << b << endl;
}

int main(){

  string s;
  cin >> s;

  int len = s.length();
  
  //()ない時
  bool f = false;
  REP(i,len){
    if(s[i] == '('){
      f = true;
    }
  }

  int a, b;

  if(f){
    string tmp = "", tmp2 = "";
    int cnt = 0, cnt2 = 0;
    int p_idx;
    bool kakko = false;
    REP(i,len-1){
      if(s[i] == '('){
        kakko = true;
        cnt2 = i - p_idx - 1;
        continue;
      }else if(s[i] == '.'){
        cnt = (len-1) - i - 2;
        p_idx = i;
      }else{
        tmp += s[i];
        if(!kakko){
          tmp2 += s[i];
        }
      }
    }
    a = stoi(tmp) - stoi(tmp2);
    b = pow(10, cnt) - pow(10, cnt2);
    kiyaku(a, b);
  }else{
    string tmp = "";
    int cnt = 0;
    REP(i,len){
      if(s[i] == '.'){
        cnt = len - 1 - i;
      }else{
        tmp += (s[i]);
      }
    }
    b = pow(10, cnt);
    a = stoi(tmp);
    kiyaku(a, b);
    return 0;
  }

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3380
final_rank: 1

code_snippet: #include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdio>

using namespace std;

double _stod(string str) {
    double integ = 0, dec = 0;
    int i, len = str.size();
    for (i = 0; i < len; ++i) { //??´??°??¨
        if (str[i] == '.')
            break;
        integ = integ * 10 + (str[i] - '0');
    }
    i++;
    double k = 10;
    for (; i < len; ++i, k *= 10) { //????????§
        if (str[i] == '(')
            break;
        dec += (str[i] - '0') / k;
    }
    i++;
    int loop = 0, ll = 0;
    for (; i < len; ++i) {
        if (str[i] == ')')
            break;
        loop = loop * 10 + (str[i] - '0');
        ll++;
    }
    k /= 10;
    for (int i = 0; i < 100; ++i) {
        for (int j = 0; j < ll; ++j)
            k *= 10;
        dec += loop / k;
    }
    
    return integ + dec;
}

void _div(long long &n, long long &m) {
    while (1) {
        bool check = true;
        for (int i = 2; i <= m; ++i) {
            if (n % i == 0 && m % i == 0) {
                n /= i;
                m /= i;
                check = false;
            }
        }
        if (check)
            break;
    }
}

int main() {
    string str;
    cin >> str;
    
    double base = _stod(str);
    
    long long nmax = 1e12, mmax = 1e6, nans, mans;
    double emin = 1e9, e;
    for (int m = 1; m <= mmax; ++m) { //??????
        long long l = 1, r = nmax, mid;
        while (r - l > 1) {
            mid = (l + r) / 2;
            if (fabs((double)l / m - base) < fabs((double)r / m - base))
                r = mid;
            else
                l = mid;
        }
        e = fabs((double)r / m - base);
        if (emin > e) {
            emin = e;
            nans = r;
            mans = m;
        }
    }
    
    _div(nans, mans);
    
    printf("%lld/%lld\n", nans, mans);
    
    return 0;
}
accuracy: 0.26666666666666666
time_ms: 320
memory_kb: 3216
final_rank: 2

code_snippet: #include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdio>

using namespace std;

double _stod(string str) {
    double integ = 0, dec = 0;
    int i, len = str.size();
    for (i = 0; i < len; ++i) { //??´??°??¨
        if (str[i] == '.')
            break;
        integ = integ * 10 + (str[i] - '0');
    }
    i++;
    double k = 10;
    for (; i < len; ++i, k *= 10) { //????????§
        if (str[i] == '(')
            break;
        dec += (str[i] - '0') / k;
    }
    i++;
    int loop = 0, ll = 0;
    for (; i < len; ++i) {
        if (str[i] == ')')
            break;
        loop = loop * 10 + (str[i] - '0');
        ll++;
    }
    k /= 10;
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < ll; ++j)
            k *= 10;
        dec += loop / k;
    }
    
    return integ + dec;
}

void _div(long long &n, long long &m) {
    while (1) {
        bool check = true;
        for (int i = 2; i <= m; ++i) {
            if (n % i == 0 && m % i == 0) {
                n /= i;
                m /= i;
                check = false;
            }
        }
        if (check)
            break;
    }
}

int main() {
    string str;
    cin >> str;
    
    double base = _stod(str);
    
    long long nmax = 1e12, mmax = 1e6, nans, mans;
    double emin = 1e9, e;
    for (int m = 1; m <= mmax; ++m) { //??????
        long long l = 1, r = nmax, mid;
        while (r - l > 1) {
            mid = (l + r) / 2;
            if (fabs((double)l / m - base) < fabs((double)r / m - base))
                r = mid;
            else
                l = mid;
        }
        e = fabs((double)r / m - base);
        if (emin > e) {
            emin = e;
            nans = r;
            mans = m;
        }
    }
    
    _div(nans, mans);
    
    printf("%lld/%lld\n", nans, mans);
    
    return 0;
}
accuracy: 0.26666666666666666
time_ms: 310
memory_kb: 3284
final_rank: 4

code_snippet: #include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdio>

using namespace std;

double _stod(string str) {
    double integ = 0, dec = 0;
    int i, len = str.size();
    for (i = 0; i < len; ++i) { //??´??°??¨
        if (str[i] == '.')
            break;
        integ = integ * 10 + (str[i] - '0');
    }
    i++;
    double k = 10;
    for (; i < len; ++i, k *= 10) { //????????§
        if (str[i] == '(')
            break;
        dec += (str[i] - '0') / k;
    }
    i++;
    int loop = 0, ll = 0;
    for (; i < len; ++i) {
        if (str[i] == ')')
            break;
        loop = loop * 10 + (str[i] - '0');
        ll++;
    }
    k /= 10;
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < ll; ++j)
            k *= 10;
        dec += loop / k;
    }
    
    return integ + dec;
}

void _div(long long &n, long long &m) {
    while (1) {
        bool check = true;
        for (int i = 2; i <= m; ++i) {
            if (n % i == 0 && m % i == 0) {
                n /= i;
                m /= i;
                check = false;
            }
        }
        if (check)
            break;
    }
}

int main() {
    string str;
    cin >> str;
    
    double base = _stod(str);
    
    long long nmax = 1e10, mmax = 1e6, nans, mans;
    double emin = 1e9, e;
    for (int m = 1; m <= mmax; ++m) { //??????
        long long l = 1, r = nmax, mid;
        while (r - l > 1) {
            mid = (l + r) / 2;
            if (fabs((double)l / m - base) < fabs((double)r / m - base))
                r = mid;
            else
                l = mid;
        }
        e = fabs((double)r / m - base);
        if (emin > e) {
            emin = e;
            nans = r;
            mans = m;
        }
    }
    
    _div(nans, mans);
    
    printf("%lld/%lld\n", nans, mans);
    
    return 0;
}
accuracy: 0.26666666666666666
time_ms: 260
memory_kb: 3272
final_rank: 3