一、问题描述
求最大公约数
python
class Fraction:
def __init__(self, a, b):
self.a = a
self.b = b
x = self.gcd(a, b)
self.a /= x
self.b /= x
def gcd(self, a, b):
while b >0:
r = a % b
a = b
b = r
return a
def zgs(self, a, b):
x = self.gcd(a, b)
return a / x * b
def __add__(self, other):
a = self.a
b = self.b
c = other.a
d = other.b
fenmu = self.zgs(b, d)
fenzi = a * fenmu / b + c * fenmu / d
return Fraction(fenzi, fenmu)
def __str__(self):
return "%d/%d" % (self.a, self.b)
a = Fraction(1, 3)
b = Fraction(1, 2)
print(a+b)
二、结果展示
python
5/6