/ Python

Python: 练习作品-最小公倍数和最大公约数

看到一个练习题就拿来练练打字,尽量往复杂了做。

#!/usr/bin/env python
#coding=utf-8
"""
 * Practice : 最大公约数最大公倍数
 * Parameter: 传入两个正整数,返回所求两个结果
 * Coder    : AT
 * Date     : 2016-04-15
 * Blog     : https://ghost.atibm.com/2016/04/15/python-lian-xi-zuo-pin-zui-xiao-gong-bei-shu-he-zui-da-gong-yue-shu/
"""

class Math:

    def __init__(self, NumM=1, NumN=1):
        try:
            NumM = int(NumM)
        except:
            NumM = 1
        try:
            NumN = int(NumN)
        except:
            NumN = 1
        if NumM <= 0:
            NumM = 1
        if NumN <= 0:
            NumN = 1
        self.NumM = NumM
        self.NumN = NumN

    def MinMul(self):
        TmpNumList = self.Cmp()
        MaxNum = TmpNumList[0]
        MinNum = TmpNumList[1]
        MinMulNum = MaxNum * MinNum
        for i in range(MinNum):
            i += 1
            TmpNum = MaxNum * i
            if TmpNum % MinNum == 0:
                MinMulNum = TmpNum
                break
        return MinMulNum

    def MaxDiv(self):
        TmpNumList = self.Cmp()
        MaxNum = TmpNumList[0]
        MinNum = TmpNumList[1]
        MaxDivNum = MinNum
        for i in range(MinNum, 0, -1):
            if MaxNum % i == 0 anf MinNum % i ==0:
                MaxDivNum = i
                break
        return MaxDivNum

    def Cmp(self):
        if self.NumM > self.NumN:
            return [self.NumM, self.NumN]
        else:
            return [self.NumN, self.NumM]

    def help(self):
        print '''about class Math Object and Function ...'''

if __name__ == "__main__":
    Math().help()
    while True:
        NumM = raw_input("请输入数字M:")
        NumN = raw_input("请输入数字N:")
        DoMath = Math(NumM, NumN)
        MinMulNum = DoMath.MinMul()
        MaxDivNum = DoMath.MaxDiv()
        print "%s 和 %s 的最小公倍数为%d,最大公约数为%d"%(NumM, NumN, MinMulNum, MaxDivNum)
        if raw_input("Enter to continue,at for quit :") == "at":
            break