用递归,感觉还行..

学英语

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

#include <bits/stdc++.h>

using namespace std;


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"};


string englishnum(int num)
{
    if(num>=0&&num<=19)
        return other[num];
    if(num>=20&&num<=99){
        if(num%10==0)
            return ften[num/10];
        return ften[num/10]+" "+englishnum(num%10);
    }
    if(num>=100&&num<=999){
        if(num%100==0){
            return other[num/100]+" hundred";
        }else{
            return other[num/100]+" hundred and "+englishnum(num%100);
        }
    }
    if(num>=1000&&num<999999){
        if(num%1000==0)
            return other[num/1000]+" thousand";
        return englishnum(num/1000)+" thousand "+englishnum(num%1000);
    }
    if(num>=1000000&&num<9999999)
    {
        if(num%1000000==0)
            return other[num/1000000]+" million";
        return other[num/1000000]+" million "+englishnum(num/1000%1000)+" thousand "+englishnum(num%1000);
    }
    if(num>=10000000){
        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 n(0);
    while(cin>>n){

        cout<<englishnum(n)<<endl;
        n=0;
    }
    return 0;
}












全部评论
2003输出的应该是“two thousand and three”然而这种方法得到的是“two thousand three”虽然测试用例里没有
2 回复 分享
发布于 2021-09-06 20:18

相关推荐

06-25 16:53
门头沟学院 Java
人力小鱼姐:简历可以直接用飞书模板 模拟面试可以试试ai,现在好多都还是免费阶段 像Sugar云面、多面鹅都不错,主要看面试后自己能不能复盘出有效信息
为了找工作你花了哪些钱?
点赞 评论 收藏
分享
05-13 02:01
已编辑
惠州学院 前端工程师
安静的少年在求佛:建议把公司名字写到标题。以后有人想搜就能直接搜到
点赞 评论 收藏
分享
05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
13
收藏
分享

创作者周榜

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