frac_top1 = fractop1
frac_top2 = fractop2
frac_bottom1 = fracbottom1
frac_bottom2 = fracbottom2
if frac_bottom1 == frac_bottom2:
frac_top3,frac_bottom3 = frac_top1+frac_top2,frac_bottom1
else:
frac = frac_top1,frac_bottom1
frac_top1*=frac_bottom2
frac_bottom1*=frac_bottom2
frac_top2*=frac
frac_bottom2*=frac
frac_top3 = frac_top1+frac_top2
frac_bottom3 = frac_bottom1
return (frac_top3,frac_bottom3) def lcm(x, y): # 最小公倍数
s= x*y
while y:
x, y = y, x%y
return s//x
def fracAdd(num1, deno1, num2, deno2):
if deno1 == deno2: # 分母相同
return((num1+num2, deno1))
else: # 分母不相同
comm = lcm(deno1, deno2)
new_num1 = comm // deno1 * num1
new_num2 = comm // deno2 * num2
new_num = new_num1 + new_num2
return((new_num,comm))
if __name__ == '__main__':
print(fracAdd(1, 2, 5, 4))
import random as r
class Solution:
def fracAdd(self, mo_1, den_1, mo_2, den_2):
"""
:type mo_1, den_1, mo_2, den_2: int
:rtype: tuple
"""
if den_1 == 0 or den_2 == 0:
return "请输入分母非零的分数"
else:
den_3 = den_1 * den_2
temp_1 = mo_1 * den_2
temp_2 = mo_2 * den_1
mo_3 = temp_1 + temp_2
if mo_3 == 0:
return(0, 0)
else:
factor= self.judge(mo_3, den_3)
mo_3 //= factor
den_3 //= factor
return (mo_3, den_3)
def judge(self, x, y):
a = min(abs(x), abs(y))
factor = 1
for i in range(2, (a+3)//2):
if x % i == 0 and y % i == 0:
factor = i
if x * y > 0:
return factor
elif x * y < 0:
return -factor
# 测试用例
a = Solution()
range_area = [-100,100]
n = 100
for i in range(n):
mo_1 = r.randint(range_area,range_area)
den_1 = r.randint(range_area,range_area)
mo_2 = r.randint(range_area,range_area)
den_2 = r.randint(range_area,range_area)
print(mo_1, den_1, mo_2, den_2, a.fracAdd(mo_1, den_1, mo_2, den_2), end='\n')
本帖最后由 黎芃荨 于 2017-11-18 22:06 编辑
黎芃荨 发表于 2017-11-18 22:03
{:9_218:} 本帖最后由 776667 于 2017-11-19 12:12 编辑
def fracAdd(a,b,c,d):
def gcd(x,y):
if y == 0:
return x
return gcd(y,x%y)
a *= d
c *= b
b *= d
numerator = (a+c)//gcd(sorted(),sorted())
b = b//gcd(sorted(),sorted())
return (numerator,b) Elastcio 发表于 2017-11-18 20:27
# 加上约分了
def lcm(x, y): # 最小公倍数
s= x*y
while y:
x, y = y, x%y
return s//x
def gcd(x,y): # 最大公约数
return x if y == 0 else gcd(y, x%y)
def fracAdd(num1, deno1, num2, deno2):
if deno1 == deno2: # 分母相同
return((num1+num2, deno1))
else: # 分母不相同
comm = lcm(deno1, deno2)
new_num1 = comm // deno1 * num1
new_num2 = comm // deno2 * num2
new_num = new_num1 + new_num2
igcd = gcd(new_num, comm)
return((new_num//igcd,comm//igcd))
if __name__ == '__main__':
print(fracAdd(1, 2, 6, 4)) # (2,1) def gcd(x, y):
# 计算最大公约数,运用欧几里得算法,即两个数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。
return x if y == 0 else gcd(y, x % y)
def fracAdd(n_1, d_1, n_2, d_2): # 输入 分子1, 分母1, 分子2, 分母2
# 通分成同分母,然后分子相加
d_3 = d_1 * d_2
n_1 *= d_2
n_2 *= d_1
n_3 = n_1 + n_2
# 除以最大公约数
factor = gcd(d_3, n_3)
d_3 //= factor
n_3 //= factor
return n_3, d_3
n_1, d_1, n_2, d_2 = map(int, input('请分别输入分子1, 分母1, 分子2, 分母2:').split(','))
print(fracAdd(n_1, d_1, n_2, d_2)) SixPy 发表于 2017-11-14 12:48
写的逻辑挺清楚的,容易看懂。赞 def fenzi(a,b,c,d):
x=a*d+c*b
y=b*d
if x>y:
n=y
else:
n=x
while True:
if x%n==0 and y%n==0:
x=x/n
y=y/n
n=n-1
if n==0:
break
return int(x),int(y)
s=fenzi(18,24,1,1)
print(s) def fracAdd(分子1, 分母1, 分子2, 分母2):
a = 分母1*分母2
b = 分子1*分母2 + 分子2*分母1
#目标分数为b/a,需约分
n = min(abs(b),abs(a))
for i in range(n,0,-1):
if a%i==0 and b%i==0:
a = a//i
b = b//i
break
return (b, a)
print(fracAdd(1, 6, 2, 3))
#输出:(5, 6) def fracAdd(mole_0,deno_0,mole_1,deno_1):
denominator=1
while denominator%deno_0 != 0 or denominator%deno_1 != 0:
denominator+=1
multiple_0=denominator/deno_0
multiple_1=denominator/deno_1
molecule_0=multiple_0*mole_0
molecule_1=multiple_1*mole_1
result_mole=int(molecule_0+molecule_1)
result_deno=denominator
return (result_mole,result_deno)
print('fracAdd(1,6,2,3)')
print(fracAdd(1,6,2,3))
mole_0=int(input("please enter the molecule of the first number:"))
deno_0=int(input("please enter the denominator of the first number:"))
mole_1=int(input("please enter the molecule of the second number:"))
deno_1=int(input("please enter the denominator of the first number:"))
print(fracAdd(mole_0,deno_0,mole_1,deno_1)) def fun124(*arg):
mol1 = arg
m = den1 = arg
mol2 = arg
n = den2 = arg
#求分母最小公倍数
if m > n:
m, n = n, m
r = m%n
while r:
m = n
n = r
r = m%n
str1 = "%d/%d" % ((mol1 * den2 + mol2 * den1) / n, den1 * den2 / n )
return str1
print(fun124(1,6,5,3))
from math import gcd
def fracAdd(a,b,c,d):
n1,n2=a*d+b*c,b*d
n=gcd(n1,n2)
return n1//n,n2//n def fracAdd(n1,d1,n2,d2):
def gcd(a, b):
return (a if b == 0 else gcd(b, a % b))
n3,d3=n1*d2+n2*d1,d1*d2
return n3//gcd(n3,d3),d3//gcd(n3,d3) def fracAdd(a, b, c, d):
if b % d == 0:
k = b // d
return (a + c * k, b)
elif d % b == 0:
k = d // b
return (a * k + c, d)
else:
return (a * d + b * c, b * d)
fracAdd
print(fracAdd(1, 6, 2, 3))
print(fracAdd(3, 4, 5, 8))
print(fracAdd(3, 5, 5, 8))
页:
1
[2]