// 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
}
}
}