题解 | #学英语#C++版本(简介)

学英语

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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string num1[25] = {"zero", "one", "two", "three", "four", "five",
                "six", "seven", "eight", "nine", "ten",
                "eleven", "twelve", "thirteen", "fourteen", 
                "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string num2[15] = {"0","0","twenty","thirty", "forty","fifty","sixty","seventy","eighty","ninety"};

vector<string> words;


void read1(int x)
{
    if(x >= 20)
    {
        words.push_back(num2[x / 10]);
        x %= 10;
        if(x)
            words.push_back(num1[x]);
    }
    else
        words.push_back(num1[x]); //读一个1~20以内的数
}

//读1000以内的数
void read2(int x)
{
    if(x >= 100)
    {
        words.push_back(num1[x / 100]);
        words.push_back("hundred");
        x %= 100;
        if(x)
        {
            words.push_back("and");
            read1(x);
        }
    }
    else
        read1(x); //读一个100以内的数
}

int main()
{
    long long int n;
    cin >> n;
    //拆分
    int a = n % 1000; //thousand内
    int b = (n / 1000) % 1000; //million内
    int c = (n / 1000000) % 1000; //billion内
    int d = n / 1000000000; //billion外

    if(d)
    {
        read2(d);
        words.push_back("billion");
    }
    if(c)
    {
        read2(c);
        words.push_back("million");
    }
    if(b)
    {
        read2(b);
        words.push_back("thousand");
    }
    if(a)
        read2(a);

    //输出
    for(int i = 0; i < words.size(); i++)
    {
        if(i < words.size() - 1)
            cout << words[i] << " ";
        else
            cout << words[i];
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
Cherrycola01:0实习 0项目 约等于啥也没有啊 哥们儿这简历认真的吗
点赞 评论 收藏
分享
06-02 15:53
阳光学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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