Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

実数既約分数化

  実数のうち、小数部が循環するものと有限桁のものは分数として表すことができます。

分数で表すことができる実数が与えられたとき、その実数と等しい既約分数（それ以上約分できない分数）を出力するプログラムを作成せよ。

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



Sample input:
0.(3)


Sample output:
1/3

Submission status: Wrong Answer

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n
def f(v):
    d = v.find(".")
    a = int(v[:d]+v[d+1:])
    n = 10**(len(v)-d-1)
    if a == 0:
        return 0, 1
    g = gcd(n, a)
    return a/g, n/g
s = input()
l = s.find("(")
r = s.find(")")
if l == -1:
    print("%d/%d" % f(s))
else:
    va, na = f(s[:l])
    vb, nb = f("0." + "0"*(l-2) + s[l+1:r])
    t = r-l-1
    vb *= 10**t
    nb *= 10**t - 1

    ng = gcd(na, nb)
    va *= nb; vb *= na
    v = va+vb; n = na*nb
    g = gcd(v, n)
    v /= g; n /= g
    print("%d/%d" % (v, n))

Submission status: Wrong Answer

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n
def f(v):
    d = v.find(".")
    if d == -1:
        return int(v), 1
    a = int(v[:d]+v[d+1:])
    n = 10**(len(v)-d-1)
    if a == 0:
        return 0, 1
    g = gcd(n, a)
    return a/g, n/g
s = input()
l = s.find("(")
r = s.find(")")
if l == -1:
    res = f(s)
else:
    va, na = f(s[:l])
    vb, nb = f("0." + "0"*(l-2) + s[l+1:r])
    t = r-l-1
    vb *= 10**t
    nb *= 10**t - 1

    ng = gcd(na, nb)
    va *= nb; vb *= na
    v = va+vb; n = na*nb
    g = gcd(v, n)
    v /= g; n /= g
    res = v, n
print("%d/%d" % res)

Submission status: Wrong Answer

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n
def f(v):
    d = v.find(".")
    if d == -1:
        return int(v), 1
    a = int(v[:d]+v[d+1:])
    n = 10**(len(v)-d-1)
    if a == 0:
        return 0, 1
    g = gcd(n, a)
    return int(a/g), int(n/g)
s = input()
l = s.find("(")
r = s.find(")")
if l == -1:
    res = f(s)
else:
    aa, na = f(s[:l])
    assert l>=2
    ab, nb = f("0." + "0"*(l-2) + s[l+1:r])
    t = r-l-1
    a = (10**t - 1)*aa*nb + 10**t*ab*na
    n = (10**t - 1)*na*nb

    g = gcd(a, n)
    res = a/g, n/g
print("%d/%d" % res)

Submission status: Wrong Answer

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n
def f(v):
    d = v.find(".")
    if d == -1:
        return int(v), 1
    a = int(v[:d]+v[d+1:])
    n = 10**(len(v)-d-1)
    if a == 0:
        return 0, 1
    g = gcd(n, a)
    return int(a/g), int(n/g)
s = input()
l = s.find("(")
r = s.find(")")
if l == -1:
    res = f(s)
else:
    assert l>=2 and r == len(s)-1
    aa, na = f(s[:l])
    ab, nb = f("0." + "0"*(l-2) + s[l+1:r])
    t = r-l-1
    a = (10**t - 1)*aa*nb + 10**t*ab*na
    n = (10**t - 1)*na*nb

    g = gcd(a, n)
    res = int(a/g), int(n/g)
print("%d/%d" % res)

Submission status: Accepted

Solution code:
def gcd(m, n):
    r = m % n
    return gcd(n, r) if r else n
def f(v):
    d = v.find(".")
    if d == -1:
        return int(v), 1
    a = int(v[:d]+v[d+1:])
    n = 10**(len(v)-d-1)
    if a == 0:
        return 0, 1
    g = gcd(n, a)
    return int(a/g), int(n/g)
s = input()
d = s.find(".")
l = s.find("(")
r = s.find(")")
if l == -1:
    res = f(s)
else:
    assert l>=2 and r == len(s)-1
    aa, na = f(s[:l])
    ab, nb = f("0." + "0"*(l-d-1) + s[l+1:r])
    t = r-l-1
    a = (10**t - 1)*aa*nb + 10**t*ab*na
    n = (10**t - 1)*na*nb

    g = gcd(a, n)
    res = int(a/g), int(n/g)
print("%d/%d" % res)

Submission status: Accepted

Solution code:
from math import gcd

s = input()
x, y = s.split(".")
if "(" in s:
  da = y.index("(")
  db = y.index(")") - 1
  ya, b = y.split("(")
  lb = len(b) - 1
  a = int(x + ya)
  b = int(b[:-1])
  deco = a * (10 ** db - 10 ** (db - lb)) + b * 10 ** da
  nume = 10 ** da * (10 ** db - 10 ** (db - lb))
  div = gcd(deco, nume)
  print(deco // div, nume // div, sep="/")
else:
  da = len(y)
  a = int(x + y)
  deco = a
  nume = 10 ** da
  div = gcd(deco, nume)
  print(deco // div, nume // div, sep="/")

Submission status: Accepted

Solution code:
def gcd(x, y):
    return gcd(y, x%y) if y else x

def printV(x, y):
    g = gcd(x, y)
    print(str(x//g) + "/" +  str(y//g))

S = input()
all = ""
sub = ""
p = -1

for i in range(len(S)):
    if S[i] == '.':
        o = i
    elif S[i] == '(':
        p = i
        sub = all
    elif S[i] != ')':
        all += S[i]

d = len(S) - o - 1
l = p - o - 1
if p == -1:
    printV(int(all), 10**d)
else:
    d -= 2 # for ()
    printV(int(all) - int(sub), 10**d - 10**l)

Submission status: Accepted

Solution code:
from math import gcd


def solve(p, q):
    GCD = gcd(p, q)
    p = p // GCD
    q = q // GCD
    print(p, q, sep="/")


s = input()
x, y = s.split('.')
if(y[-1] != ')'):
    p = int(x) * pow(10, len(y)) + int(y)
    q = pow(10, len(y))
    solve(p, q)

else:
    y, z = y.split('(')[0], y.split('(')[1][:-1]
    a = int(x+y+z)
    b = int(x+y)
    solve(a-b, (pow(10, len(z))-1)*pow(10, len(y)))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    e = LI()
    d = defaultdict(int)
    for i in e:
        d[i] += 1
    for i in d.values():
        if i != 2:
            print("no")
            break
    else:
        print("yes")
    return

#B
def B():
    n = I()
    a = LI()
    a.sort()
    ans = -float("inf")
    for c in range(n):
        for d in range(c):
            m = a[c]-a[d]
            for i in range(n)[::-1]:
                if i != c and i != d:
                    e = i
                    break
            for i in range(e)[::-1]:
                if i != c and i != d:
                    b = i
                    break
            ans = max(ans, (a[e]+a[b])/m)
    print(ans)
    return

#C
def C():
    def gcd(a,b):
        if a == 0:
            return b
        return gcd(b%a, a)

    s = input()
    n = len(s)
    if s.count("(") == 0:
        s = float(s)
        b = 10**(n-2)
        a = round(s*b)
        g = gcd(a,b)
        a //= g
        b //= g
    else:
        n = s.find("(")
        t = float(s[:n])
        b = 10**(n-2)
        a = round(t*b)
        g = gcd(a,b)
        a //= g
        b //= g
        l = (s.find("(")-s.find(".")-1)
        s = s[n+1:-1]
        m = len(s)
        c = round(float(s))
        d = (10**m-1)*10**l
        g = gcd(c,d)
        c //= g
        d //= g
        a = a*d+b*c
        b = b*d
        g = gcd(a,b)
        a //= g
        b //= g
    print(str(a)+"/"+str(b))
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    C()