题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

#include<bits/stdc++.h>
using namespace std;
int n;
string a[] = {"a0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; //20以内的单词表
string b[] = {"b0", "b1", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; //整十单词表
const string hun = "hundred";
const string tho = "thousand";
const string mil = "million";
const string bil = "billion"; //连接词表
string engn(int n);
signed main() {
    while (~scanf("%d", &n)) {
        string ans = engn(n);
        cout << ans << endl;
    }
    return 0;
}
string engn(int n) {
    if (n < 20)
        return a[n];//若n小于20直接从表中输出
    else if (n < 100) {
        if (n % 10 == 0) //若n能被10整除
            return b[n / 10]; //从整十表中输出
        else
            return b[n / 10] + " " + engn(n % 10); //分别计算十位和个位
    } else if (n < 1000) {
        if (n % 100 == 0)
            return a[n / 100] + " " + hun; //同理,若整百直接输出数字加百
        else
            return engn(n / 100) + " " + hun + " and " + engn(n %
                    100); //分别计算百位,十位和个位
    } else if (n < 1000000) { //对于一百万以下的数字同理处理
        if (n % 1000 == 0)
            return engn(n / 1000) + " " + tho;
        else
            return engn(n / 1000) + " " + tho + " " + engn(n % 1000);
    } else {
        if (n % 1000000 == 0)
            return engn(n / 1000000) + " " + mil;
        else
            return engn(n / 1000000)/*百万位上*/ + " " + mil + " " + engn(
                       n / 1000 % 1000)/*百万位到千位*/ + " " + tho + " " + engn(
                       n % 1000); /*千位到个位*/
    }
    return "err";
}

全部评论

相关推荐

04-27 08:59
常州大学 Java
牛客139242382号:《两门以上汇编语言》
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务