题解 | #学英语#

学英语

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)

全部评论

相关推荐

11-08 16:53
门头沟学院 C++
投票
滑模小马达:第三个如果是qfqc感觉还行,我签的qfkj搞电机的,违约金也很高,但公司感觉还可以,听说之前开过一个试用转正的应届生,仅供参考。
点赞 评论 收藏
分享
object3:开始给部分🌸孝子上人生第一课了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务