7-1 sdut-分数的化简 -- Fraction的使用

做法1: 使用内置函数Fraction

from fractions import Fraction

while True:
    try:
        a,b=map(int,input().split("/"))
        print(Fraction(a,b))
    except:
        break


做法2:手写gcd

知识点:gcd简单应用

def gcd(a,b):
    if a%b==0:
        return b
    else :
        return gcd(b,a%b)
while True:
    try:
        a,b=map(int,input().split("/"))
        cnt=gcd(a,b)
        if a==0:
            print("0")
        else:
            print("{}/{}".format(a//cnt,b//cnt))
    except:
        break

posted @ 2022-05-31 16:53  kingwzun  阅读(174)  评论(0编辑  收藏  举报