题解 | #学英语#

学英语

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

当然要拍照记录啊
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<string>>ans = { {"one","two","three","four","five","six","seven","eight","nine"},{"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"} };
vector<string>ans1 { "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };
vector<string>ans2 = { "thousand","million","billion" };

string daan(int a) {
    string s;
    if (a / 100 != 0 )
        s += ans[0][a / 100 - 1] + ' ' + "hundred";
    if (a / 100 != 0 && a % 100 != 0) {
        s += ' ';
        s += "and";
        s += ' ';
    }
    if (a % 100 > 9 && a % 100 < 20)
        s += ans1[a % 100 - 10];
    else {
        if (a % 100 > 19)
            s += ans[1][(a % 100) / 10 - 2];
        if(a % 100 > 19&&a % 10 != 0)
            s+=' ';
        if (a % 10 != 0)
            s += ans[0][a % 10 - 1];
    }
    return s;
}

int main() {
    long n;
    cin >> n;
    int a, num = -1;
    vector<string>vec;
    while (n) {
        a = n % 1000;
        vec.push_back(daan(a));
        num++;
        n /= 1000;
    }
    for (; num > 0; num--)
        cout << vec[num] << ' ' << ans2[num - 1] << ' ';
    cout << vec[0];
    return 0;
}


全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务