求最大公约数的两种算法与重载

求最大公约数的两种方法(含递归方法)

//  x和y的最大公约数跟 y%x和的公约数是一样的
//  求最大公约数的全局函数
//  Greatest Common Divisor
//func gcb(x: Int, _ y: Int) -> Int {
//    var a = x < y ? x: y
//    while a > 1 {
//        if x % a == 0 && y % a == 0 {
//            return a
//        }
//        a -= 1
//    }
//    return 1
//}


//短除法,欧几里德算法

    /**
    短除法,求两个数的最大公约数
    - parameter x: 参数1
    - parameter y: 参数2
    - returns: 返回最大公约数
    */
func gcb(x: Int, _ y: Int) -> Int {
    if x > y {
        return gcb(y, x)
    }
    else if y % x != 0{
        return gcb(y % x, x)
    }
    else {
        return x
    }
}



面对初始化了的枚举你会如何写代码?

enum Suite: String {
    case Spade = "♠️",Heart = "♥️",Club = "♣️",Dimond = "♦️"
}

///一张牌
class Card {
    var suite: Suite
    var face: Int
    
    init(suite: Suite,face: Int) {
        self.suite = suite
        self.face = face
    }
    
    /**
    初始化方法
     
    */
    
    
    var info: String {
        get {
            var str = suite.rawValue//rawValue原始值
            switch face {
            case 1: str += "A"
            case 11: str += "J"
            case 12: str += "Q"
            case 13: str += "K"
            default : str += "\(face)"
            }
            return str
        }
    }
}

你发现了其中的开火车式的编程方法了吗(观察simplify() normalize()这两个函数吧)


// 短除法(欧几里得算法)
// x和y的最大公约数跟y%x和x的最大公约数是一样的
// Greatest Common Divisor
func gcd(x: Int, _ y: Int) -> Int {
    if x > y {
        return gcd(y, x)
    }
    else if y % x != 0 {
        return gcd(y % x, x)
    }
    else {
        return x
    }
}

class Fraction {
    private var _num: Int
    private var _den: Int
    
    var info: String {
        get {
            return _num == 0 || _den == 1 ? "\(_num)" : "\(_num)/\(_den)"
        }
    }
    
    init(num: Int, den: Int) {
        _num = num
        _den = den
        simplify()
        normalize()
    }
    
    func add(other: Fraction) -> Fraction {
        return Fraction(num: _num * other._den + other._num * _den, den: _den * other._den)
    }
    
    func sub(other: Fraction) -> Fraction {
        return Fraction(num: _num * other._den - other._num * _den, den: _den * other._den)
    }
    
    func mul(other: Fraction) -> Fraction {
        return Fraction(num: _num * other._num, den: _den * other._den)
    }
    
    func div(other: Fraction) -> Fraction {
        return Fraction(num: _num * other._den, den: _den * other._num)
    }
    
    func normalize() -> Fraction {
        if _den < 0 {
            _num = -_num
            _den = -_den
        }
        return self
    }
    
    func simplify() -> Fraction {
        if _num == 0 {
            _den = 1
        }
        else {
            let x = abs(_num)
            let y = abs(_den)
            let g = gcd(x, y)
            _num /= g
            _den /= g
        }
        return self
    }
}

// 运算符重载(为自定义的类型定义运算符)

func +(one: Fraction, two: Fraction) -> Fraction {
    return one.add(two)
}

func -(one: Fraction, two: Fraction) -> Fraction {
    return one.sub(two)
}

func *(one: Fraction, two: Fraction) -> Fraction {
    return one.mul(two)
}

func /(one: Fraction, two: Fraction) -> Fraction {
    return one.div(two)
}

你可能感兴趣的:(求最大公约数的两种算法与重载)