You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
lavos/cap3/ex8.go

34 lines
913 B

package cap3
// Ex8 高位到低位输出正整数的数字
func Ex8(num uint64, algoType AlgoType, callback func(digit uint8)) {
switch algoType {
case AlgoTypeRecursion:
Ex8Recursion(num, callback)
default:
Ex8NoneRecursion(num, callback)
}
}
// Ex8Recursion 高位到低位输出正整数的数字(递归)
func Ex8Recursion(num uint64, callback func(digit uint8)) {
if num < 10 {
callback(uint8(num))
} else {
// 跟ex7的小区别
Ex8Recursion(num/10, callback)
callback(uint8(num % 10))
}
}
// Ex8NoneRecursion 高位到低位输出正整数的数字(非递归)
func Ex8NoneRecursion(num uint64, callback func(digit uint8)) {
digits := make([]uint8, 0, 20)
for i := num; i >= 1; i /= 10 {
digits = append(digits, uint8(i%10))
}
for i := len(digits) - 1; i >= 0; i-- {
callback(digits[i])
}
}