题解 | #学英语#

学英语

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


#include<bits/stdc++.h>
using namespace std;
map<char, string>table;
vector<string>num = {"","one","two","three","four","five", "six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen", "sixteen","seventeen","eighteen","nineteen"};
vector<string>num20 = {"twenty","thirty","forty","fifty","sixty" ,"seventy","eighty","ninety"};
vector<string>unite ={"","thousand", "million","billion"};
string getUnite(string s){
    string res;
    int n = atoi(s.c_str());
    if(n<=19)return num[n];
    int _100 = n/100;
    int _10 = n%100/10;
    int _1 = n%100%10;
    bool isHundred = false;
    if(_100!=0){
        res+=(num[_100]+ " hundred");
        isHundred = true;
    }
    if(_10!=0 || _1!=0)
        if(isHundred)res+=" and ";
    if(_10>=2){
        if(_1)
        res+=(num20[_10 - 2] + " "+ num[_1]);
        else
            res+=(num20[_10 - 2]);
    }
    else
        res+=num[n%100];


    return res;

}

int fun(){
    string s;
    cin>>s;
    vector<string>res;
   while(s.size()>3){
        res.emplace_back(s.substr(s.size()-3));
        s = s.substr(0,s.size() -3);
    }
   if(s!="")res.emplace_back(s);

   string s1;
   if(res.size()==1 &&res[0]=="0"){cout<<"zero"<<endl;}
    for(int i = res.size() -1;i>=0;i--){
         s1 += (getUnite(res[i])+" ") ;
         s1+=(unite[i] + " ");
    }
    cout<<s1<<endl;
    return 0;
}
int main() {
    fun();
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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