数学推理 | HJ82 将真分数分解为埃及分数
# 最优解 from fractions import Fraction while True: try: num = input() frac = Fraction(num) fz, fm = frac.numerator, frac.denominator res = [] while True: if fz == 1: res.append(f'1/{fm}') break elif fz == 3 and fm % 2 == 0: fz = 1 res.append('1/{}'.format(fm)) fm = fm // 2 else: res.append('1/{}'.format(fm//fz+1)) subtraction = Fraction(fz, fm) - Fraction(1, fm//fz+1) fz = subtraction.numerator fm = subtraction.denominator print('+'.join(res)) except: break
用时:1.5h
#数学逻辑#华为笔试刷题 文章被收录于专栏
高质量题: 1~40:HJ16,HJ22,HJ24,HJ26,HJ27,HJ28,HJ35,HJ37,HJ39; 40~80:HJ41,HJ42,HJ43,HJ44,HJ48,HJ50,HJ52,HJ53,HJ57,HJ61,HJ63,HJ64,HJ70,HJ71,HJ74,HJ77; 80~108:HJ82,HJ85,HJ88,HJ89,HJ93,HJ95,HJ98,HJ103,HJ107