题解 | #学英语#

学英语

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)

全部评论

相关推荐

尊嘟假嘟点击就送:加v细说,问题很大
点赞 评论 收藏
分享
11-09 01:22
已编辑
东南大学 Java
高级特工穿山甲:羡慕,我秋招有家企业在茶馆组织线下面试,约我过去“喝茶详谈”😢结果我去了发现原来是人家喝茶我看着
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务