题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
package main import ( "fmt" "strings" ) var nums1 = []string { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", } var nums2 = []string { "0", "0", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", } var word []string // 100 以内转英文 func hundred2word(n int) { if n == 0 { return } if n < 20 { word = append(word, nums1[n]) return } word = append(word, nums2[n/10]) if n % 10 != 0 { word = append(word, nums1[n%10]) } } // 1000 以内转英文 func thousand2word(n int) { if n >= 100 { word = append(word, nums1[n/100]) word = append(word, "hundred") if n % 100 != 0 { word = append(word, "and") } } hundred2word(n % 100) } func readWord(n int) { a := n % 1000 b := (n / 1000) % 1000 c := (n / 1000 / 1000) % 1000 d := n / 1000 / 1000 / 1000 if d > 0 { thousand2word(d) word = append(word, "billion") } if c > 0 { thousand2word(c) word = append(word, "million") } if b > 0 { thousand2word(b) word = append(word, "thousand") } if a > 0 { thousand2word(a) } fmt.Println(strings.Join(word, " ")) } func main() { var n int fmt.Scan(&n) readWord(n) }
// 本题输入一个整数,所以采用:fmt.Scan(&n)