题解 | #学英语#

学英语

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

#include<bits/stdc++.h>
using namespace std;

string englishnum(int num) {
    vector<string> other = {"zero", "one", "two", "three", "four", "five", "six", "seven",
                           "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
                           "fifteen", "sixteen", "seventeen", "eighteen", "ninteen"};
    vector<string> ften = {"none", "ten", "twenty", "thirty", "forty", "fifty", "sixty",
                          "seventy", "eighty", "ninety"};
    if(num>=0 && num<=19)
        return other[num];
    else if(num>=20 && num<=99) {
        if(num%10 == 0)
            return ften[num/10];
        else{
            return ften[num/10]+" "+englishnum(num%10);
        }
    }
    else if(num>=100 && num<=999) {
        if(num%100 == 0)
            return other[num/100]+" hundred";
        else{
            return englishnum(num/100)+" hundred and "+englishnum(num%100);
        }
    }
    else if(num>=1000 && num<=999999) {
        if(num%1000 == 0)
            return englishnum(num/1000)+" thousand";
        else{
            return englishnum(num/1000)+" thousand "+englishnum(num%1000);
        }
    }
    else if(num>=1000000&&num<=999999999)
    {
        if(num%1000000==0)
            return englishnum(num/1000000)+" million";
        return englishnum(num/1000000)+" million "+englishnum(num/1000%1000)+" thousand "+englishnum(num%1000);
    }
    else if(num>=1000000000){
        if(num%1000000000==0)
            return other[num/1000000000]+" billion";
        return englishnum(num/1000000000)+" billion "+englishnum(num%10000000/1000000)+" million "+englishnum(num%1000000/1000)+" thousand "+englishnum(num%1000);
    }
}

int main() {
    int num;
    while(cin>>num) {
        cout<<englishnum(num)<<endl;
    }
    return 0;
}
全部评论
大佬,nineten
1 回复 分享
发布于 2022-03-09 22:43
自定义函数当上10忆以后样例无法通过,而且目前你这个程序的最后没有返回值,无法正常运行
点赞 回复 分享
发布于 2022-11-01 14:24 贵州
403会输出four hundred and three,题目中十位为0的话是需要要省略and的
点赞 回复 分享
发布于 2023-04-01 19:54 四川

相关推荐

今天 13:29
已编辑
湖南铁道职业技术学院 后端
小红书 后端选手 n*16*1.18+签字费期权
点赞 评论 收藏
分享
jack_miller:杜:你不用我那你就用我的美赞臣
点赞 评论 收藏
分享
13 收藏 评论
分享
牛客网
牛客企业服务