以
的格式输入一个分数
,其中
。不保证分数为最简分数。
以
的格式输出结果,其中,
表示每一个埃及分数的分母。
如果存在多个解决方案,您可以输出任意一个,系统会自动判定是否正确。注意,自测运行功能可能因此返回错误结果,请自行检查答案正确性。
2/4
1/2
在这个样例中,输出
也是正确的。
8/11
1/2+1/5+1/55+1/110
import sys
from fractions import Fraction
import math
for line in sys.stdin:
tmp=line.split('/')
l,r=int(tmp[0]),int(tmp[1])
f=Fraction(l,r)
L:list[str]=[]
# 循环逼近于零
while f>0:
# 剩余分数取倒数,向上取整后就是最大埃及分数的分母
q=int(math.ceil(f.denominator/f.numerator))
# 将求得的埃及分数加入列表
L.append("1/{}".format(q))
# 减去埃及分数得到剩余分数
f-=Fraction(1,q)
print("+".join(L))
分数b/a ,分母倍数 a*i,i>=1 作差: b/a-1/a*i = c/d 若 差值 能约分,即 d != a*i,则 用差做分数,继续作差并输出1/a*i + 若 差值 不能约分,即 d == a*i,则扩大倍数,作与 1/a*(i+1)差值 分数分子为1,结束递归,说明至最后一个差数了 另有差数不能相同import sys from fractions import Fraction def fun(x,i): k = 0 a = x.denominator if x.numerator==1: print(x) elif (x-Fraction(1,a*i)).denominator == a*i: fun(x,i+1) elif (x-Fraction(1,a*i)).denominator not in [a,a*i]: print(Fraction(1,a*i),'+',sep = '',end = '') fun(x-Fraction(1,a*i),1) while True: try: x = Fraction(input()) a = x.denominator fun(x,1) except: break
# 根据数学推导出的比较快速的算法,二十位整数可秒出结果。def aiji(m, n): if m > n: return False aiji_list = [] if n % m == 0: return [n//m] else: k = n//m+1 aiji_list.append(k) aiji_list.extend(aiji(m * k - n, n * k)) return aiji_list import sys lines = [] for line in sys.stdin: line = line.strip() if line == "": break lines.append(line) for line in lines: mn = [int(i) for i in line.split('/')] m = mn[0] n = mn[1] strr_list = ['1/'+str(i) for i in aiji(m, n)] strr = '+'.join(strr_list) print(strr)
def true(s: str):
lfin = []
fz, fm = map(int, s.split('/'))
if fm % fz == 0:
lfin.append(f'1/{fm//fz}')
return lfin
while fm % fz != 0:
lfin.append(f'1/{fm // fz + 1}')
fm, fz = fm * (fm // fz + 1), (fm // fz + 1) * fz - fm
lfin.append(f'{fz}/{fm}')
return lfin
while True:
try:
print('+'.join(true(input())))
except:
break def change(a,n):
if a<1e-6: #起初没写这行电脑红温了也没跑出来,加上这行不红温了但是结果不对
return
while a<1/n:
n +=1
if a == 1 / n:
res.append('1/' + str(n))
else:
res.append('1/' + str(n))
change(a - 1 / n,n)
while True:
try:
s=list(map(int,input().split('/')))
res=[]
n=1
change(s[0]/s[1],n)
print('+'.join(res))
except:
break fz,fm1 = map(int,input().split('/'))
res = []
fm = fm1
while fm % fz !=0:
fm = (fm1//fz +1) * fm
fz = (fm1//fz +1) * fz
res.append("1/"+str(fm//fm1))
fz = fz - fm1
res.append("1/"+str(fm//fz))
print("+".join(res)) while True:
try:
z,m = map(int, input().split("/"))
result = ["1/%s"%m for i in range(z)]
print("+".join(result))
except: break def getRealFraction(ori_fra):
num1,num2 = list(map(int,ori_fra.split("/")))
basic_fraction = "1/" + str(num2)
real_fractions = ""
for i in range(num1): real_fractions += basic_fraction+"+"
return real_fractions.strip("+")
while(True):
try:
print(getRealFraction(input()))
except:
break 挨个遍历,碰到比x小的就减掉,直到x==0
from typing import List
# x = list(map(int, input().split("/")))
def gcd(x, y):
if x > y:
x, y = y, x
while y:
x, y = y, x % y
return x
def lcm(x, y):
return x * y // gcd(x, y)
def sub_fraction(x: List[int], y: List[int]):
denomi = lcm(x[1], y[1])
x[0] = denomi / x[1] * x[0]
y[0] = denomi / y[1] * y[0]
ans = [0, 0]
ans[0] = x[0] - y[0]
ans[1] = denomi
if ans[1]!=0 and ans[0]!=0 and ans[1] % ans[0] == 0:
ans[1] = ans[1] // ans[0]
ans[0] = 1
return ans
def cmp(x: List[int], y: List[int]):
tmp = x[0] / x[1] * 1.0 - y[0] / y[1] * 1.0
if tmp > 0:
return 1
elif tmp == 0:
return 0
else:
return -1
def f(x: List[int]):
if x[1] % x[0] == 0:
print(f"1/{x[1] // x[0]}")
return
i = 1
# while cmp([1, i], x) > 0:
# i += 1
while x[1] != 0 and x[0] / x[1]:
while cmp([1, i], x) > 0:
i += 1
print(f"1/{i}", end="")
x = sub_fraction(x, [1, i])
if x[0] == 1:
print(f"+{int(x[0])}/{int(x[1])}")
return
if x[1] != 0 and x[0] / x[1]:
print("+", end="")
if __name__ == '__main__':
x = list(map(int, input().split("/")))
f(x)
#思路参考斐波那契拆解法
def chaifen(a,b):
if a == 1:
l.append(b)
return
elif b%a == 0:
l.append(b//a)
return
while a>1:
d = b//a + 1
l.append(d)
return chaifen(a*d-b,b*d)
a,b = map(int,input().split('/'))
l = []
chaifen(a,b)
for i in l:
print('1'+'/'+str(i),end='')
if i!=l[-1]:
print('+',end='') def gcd(a:int,b:int):
while True:
tmp=a%b
if tmp==0:
break
else:
a,b=b,tmp
return(b)
while True:
try:
a,b=map(int,input().strip().split('/'))
if gcd(b,a)==1:
pass
else:
tmp=gcd(b,a)
a=a//tmp
b=b//tmp
if a==1:
print(str(a)+'/'+str(b))
else:
l=[]
while True:
shang=b//a
l.append(str(shang+1))
a=a*(shang+1)-b
b=b*(shang+1)
tmp=gcd(b,a)
a=a//tmp
b=b//tmp
if a==1:
l.append(str(b))
break
s='+1/'.join(l)
s='1/'+s
print(s)
except:
break # 根据楼下思路写的 真清奇啊
while 1:
try:
a,b = input().split('/')
tmp = '1/' + b
ans = [tmp for i in range(int(a))]
print('+'.join(ans))
except:
break while True:
try:
def gcd(a, b):
return b if not a%b else gcd(b, a%b)
A, B = map(int, input().split('/'))
g = gcd(A,B)
if g>1:
A /= g
B /= g
while A>1:
C = B//A
D = B%A
A = A*C+A-B
B = B*C+B
g = gcd(A,B)
if g>1:
A /= g
B /= g
print(f"1/{int(C+1)}",end='+')
print(f"1/{int(B)}")
except:
break