#include #include using namespace std;

string numberToWords(int num) { if (num == 0) { return "zero"; } string result = ""; string ones[] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; string tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; string thousands[] = {"", "thousand", "million", "billion"};

int i = 0;
while (num > 0) {
    if (num % 1000 != 0) {
        result = helper(num % 1000, ones, tens) + thousands[i] + " " + result;
    }
    num /= 1000;
    i++;
}
return result.substr(0, result.find_last_not_of(' ') + 1);

}

string helper(int num, string ones[], string tens[]) { if (num == 0) { return ""; } else if (num < 20) { return ones[num] + " "; } else if (num < 100) { return tens[num / 10] + " " + helper(num % 10, ones, tens); } else { return ones[num / 100] + " hundred " + helper(num % 100, ones, tens); } }

int main() { int n; cin >> n; cout << numberToWords(n) << endl; return 0;

题目描述请将一个数字翻译成对应的英文。输入一个自然数n0≤n≤2^31-1。输出输出这个数的英文最后不要有多余的空格。样例输入复制1111111111输出复制one billion one hundred and eleven million one hundred and eleven thousand one hundred and elevenc++代码

原文地址: http://www.cveoy.top/t/topic/h02Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录