题解 | #学英语#

学英语

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

#include <bits/stdc++.h>

using namespace std;

vector<string> ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
vector<string> tens = { "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };
vector<string> twenties = { "zero","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety" };
vector<string> hundreds = { "hundred", "thousand", "million", "billion" };
const int ihundreds[] = {(int)1e2, (int)1e3, (int)1e6, (int)1e9, (int)1e12 }; //用来划分数字的范围

string process(long long num){
    if(num <= 9) return ones[num];
    else if(num < 20) return tens[num % 10];
    else if(num < 1e2) return twenties[num / 10] + (num % 10 == 0 ? "" : " " + ones[num % 10]);
    else{
        for(int i = 0; i < 4; i++){ //大于100
            if(num < ihundreds[i + 1]){ //(i == 0 ? " and" : " ") 因为百位数和十位数之间要加and
                return process(num / ihundreds[i]) + " " 
                    + hundreds[i] 
                    + (num % ihundreds[i] ? (i == 0 ? " and " : " ") + process(num % ihundreds[i]) : "");
            }
        }
    }
    
    return "";
}

int main(){
    long long num = 0;
    while(cin >> num){
        string res = process(num);
        
        cout << res << endl;
    }
    
    return 0;
}
华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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