用递归,感觉还行..

学英语

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

相关推荐

牛客10001:问就是六个月,全国可飞,给钱就干
点赞 评论 收藏
分享
ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
评论
13
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务