新手·ing 发表于 2017-4-16 09:24:50

Python:每日一题 29(答题领鱼币)

感谢冬雪的题目!!!

python有分数类型和相应的计算方法,要求不用分数模块(真是见鬼!)编写一个简单的分数加法和减法函数,实现分数与分数,分数与整数的加减法。如

3/5 - 1/5 = 2/5

1/2 - 1/3 = 1/6

1/2 + 1/2 = 1

1- 1/3 = 2/3

函数的用法

add(a, b, c, d)

a, b为第一个分数的分子与分母

c, d 为第二个分数的分子与分母

减法类似。

给大家不符合规则的:
**** Hidden Message *****



欢迎小伙伴们,一起答题!
如果你有能力,欢迎加入我们!
已经上车老司机:@冬雪雪冬 @lumber2388779 @ooxx7788 @gopythoner
{:10_298:} 点我走上人生巅峰{:10_298:}
{:10_248:} 帅的人都上车了,而丑的人还在犹豫{:10_248:}

jerryxjr1220 发表于 2017-4-16 10:00:40

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 16 09:45:31 2017

@author: jerry_xu
"""

def fadd(a,b,c,d):
    fenzi = a*d+b*c
    fenmu = b*d
    GCD = lambda x,y:x if y==0 else GCD(y, x%y)
    gcd = GCD(fenzi,fenmu)
    fenzi /= gcd
    fenmu /= gcd
    return fenzi, fenmu

print '1/6 + 1/3 = %d/%d' % fadd(1,6,1,3)

def fminus(a,b,c,d):
    fenzi = a*d-b*c
    fenmu = b*d
    GCD = lambda x,y:x if y==0 else GCD(y, x%y)
    gcd = GCD(fenzi,fenmu)
    fenzi /= gcd
    fenmu /= gcd
    return fenzi, fenmu

print '1/2 - 1/3 = %d/%d' % fminus(1,2,1,3)

输出:
1/6 + 1/3 = 1/2
1/2 - 1/3 = 1/6

ooxx7788 发表于 2017-4-16 10:27:57

看看不符合规则的

新手·ing 发表于 2017-4-16 11:18:38

ooxx7788 发表于 2017-4-16 10:27
看看不符合规则的

{:10_262:}

zhhmms 发表于 2017-4-17 08:59:08

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 16 09:45:31 2017

@author: jerry_xu
"""

def fadd(a,b,c,d):
    fenzi = a*d+b*c
    fenmu = b*d
    GCD = lambda x,y:x if y==0 else GCD(y, x%y)
    gcd = GCD(fenzi,fenmu)
    fenzi /= gcd
    fenmu /= gcd
    return fenzi, fenmu

print '1/6 + 1/3 = %d/%d' % fadd(1,6,1,3)

def fminus(a,b,c,d):
    fenzi = a*d-b*c
    fenmu = b*d
    GCD = lambda x,y:x if y==0 else GCD(y, x%y)
    gcd = GCD(fenzi,fenmu)
    fenzi /= gcd
    fenmu /= gcd
    return fenzi, fenmu

print '1/2 - 1/3 = %d/%d' % fminus(1,2,1,3)

奔跑的小鱼 发表于 2017-4-17 11:40:53

没看懂题目
def my_add(a,b,c,d):
    return a/b+c/d
感觉这个就能实现了

冬雪雪冬 发表于 2017-4-17 11:57:30

奔跑的小鱼 发表于 2017-4-17 11:40
没看懂题目
def my_add(a,b,c,d):
    return a/b+c/d


返回的需要是分数

奔跑的小鱼 发表于 2017-4-17 12:38:51

又看了下,看懂题目了
def Dicisor(a,b):
    #最大公约数
    dicisor_list =
    return max(dicisor_list)

def my_add(a,b,c,d):
    molecular = a*d +c*b
    denominator =b*d
    result = molecular/denominator
    if result == int(result):
      print(int(result))
    else:
      dicisor = Dicisor(molecular,denominator)
      molecular_new = int(molecular/dicisor)
      denominator_new = int(denominator/dicisor)
      print("%s/%s"%(molecular_new,denominator_new))

奔跑的小鱼 发表于 2017-4-17 12:42:25

zhhmms 发表于 2017-4-17 08:59
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""


你这个递归用的牛

奔跑的小鱼 发表于 2017-4-17 12:44:37

冬雪雪冬 发表于 2017-4-17 11:57
返回的需要是分数

{:7_114:}看懂了,楼下是我的答案

余欲渔 发表于 2017-4-17 15:42:46

本帖最后由 余欲渔 于 2017-4-17 15:48 编辑

ab=input('请输入需要计算的分式,如1/5-2/8:')
f=0
def yf(aa,bb):
    for i in range(aa,1,-1):
      if (aa/i)%1==0 and (bb/i)%1==0:
            aa=int(aa/i)
            bb=int(bb/i)
            break
    if bb>1:
      return str(aa)+r'/'+str(bb)
    else :return str(aa)
            
      
def jg(a1,a2,b1,b2):
    if f:
      aa=a1*b2+a2*b1
    else:
      aa=a1*b2-a2*b1
    bb=a2*b2
    return yf(aa,bb)
      
if '-'in ab:
    a,b=ab.split(sep='-')
elif '+'in ab:
    a,b=ab.split(sep='+')
    f=1
else :
    print('输入错误')
try:
    a1,a2=
except:
    a1=int(a)
    a2=1
try:
    b1,b2=
except:
    b1=int(b)
    b2=1
print(jg(a1,a2,b1,b2))

willLin 发表于 2017-5-11 17:06:52

看看不符合规则的

solomonxian 发表于 2017-6-13 18:18:50

应该要用到最大公约数来约分,还要判断一次是不是整数
def gcd(m,n):
    """辗转相除法,寻找n和m的最大公约数"""
    # 本来还想分大小,结果发现不分也可以,因为会换过来
    while n != 0:
      m,n = n, m%n
    return m

# 用公倍数处理,再除以最大公约数就可以相加了 (a*d)/(b*d) + (c*b)/(d*b)
def fractional_add(a,b,c,d):
    """分数加法,a/b + c/d """
   
    numerator = a*d + c*b   # 分子
    denominator = b*d      # 分母
    if numerator % denominator == 0:
      # 整除情况下打印整数
      print(numerator // denominator)
    else:
      divisor = gcd(numerator, denominator)
      print("%d / %d" % (numerator/divisor, denominator/divisor))

def fractional_minus(a,b,c,d):
    """分数减法, a/b - c/d"""
   
    numerator = a*d - c*b
    denominator = b*d
    if numerator % denominator == 0:
      # 整除情况下打印整数
      print(numerator // denominator)
    else:
      divisor = gcd(numerator, denominator)
      print("%d / %d" % (numerator/divisor, denominator/divisor))

keyi198707 发表于 2017-6-14 14:48:57

看看

张大象 发表于 2017-9-18 17:23:58

def yf(m,n):
        for i in range(2,m):
                if m%i == 0 and n%i == 0:
                        m,n = m//i,n//i
                        yf(m,n)
        return m,n

def add(a, b, c, d):
        x = b*d
        y = a*d
        z = c*b
        count = 0
        s = y + z
        s,x = yf(s,x)
        print('%s/%s'%(s,x))

renze 发表于 2017-9-18 21:09:28

def add_f(a,b,c,d):
    e = a*d+b*c
    f = b*d
    return e ,f


def sub_f(a,b,c,d):
    e = a*d-b*c
    f = b*d
    return e ,f

gausser 发表于 2017-9-23 20:07:22

from __future__ import division

# Get a and b 's the Maximum common divisor
def Max_Common_Divisor(a, b):
    while b != 0:
      c = a % b
      a = b
      b = c
    return a# a is max common divisor

# Get a and b's the least common multiple
def Min_Common_Multiple(a, b):
    return a * b / a

# Fractional Addition
# a is the first fraction's molecular, b is the first fraction's Denominator
# c is the second fraction's molecular, d is the second fraction's Denominator
# return the molecular and Denominator of Fraction_Add_Fraction's Add and Minus
def Add_Minus_Of_Fraction_Fraction(a, b, c, d):
    list = []
    New_Multiple = Min_Common_Multiple(b, d)
    b_Business = New_Multiple / b
    d_Business = New_Multiple / d
   
    a = a * b_Business
    c = c * d_Business
   
    # Get Add of Fractions
    Add_Molecular = a + c
    Add_Divisor = Max_Common_Divisor(New_Multiple, Add_Molecular)
    list.append(int(Add_Molecular / Add_Divisor))
    list.append(int(New_Multiple / Add_Divisor))
   
    # Get Minus of Fractions
    Minus_Molecular = a - c
    Minus_Divisor = Max_Common_Divisor(New_Multiple, Minus_Molecular)
    list.append(int(Minus_Molecular / Minus_Divisor))
    list.append(int(New_Multiple / Minus_Divisor))
   
    print "%d/%d + %d/%d = %d/%d" % (a, b, c, d, list, list)
    print "%d/%d - %d/%d = %d/%d" % (a, b, c, d, list, list)

石小龙 发表于 2017-11-20 17:50:43

不会做

wyh2006 发表于 2017-11-20 22:54:03

学习学习!

achencan 发表于 2017-12-4 19:30:58

1
页: [1] 2 3
查看完整版本: Python:每日一题 29(答题领鱼币)