原题
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10^100).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
思路
题意:给你一个非负整数N,你的目标是求出N各个位数的和。和的每一位数字用英文输出。
注意这里的N可能超级大。
题解
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
string n;
cin >> n;
int sum = 0;
for (auto s : n) {
sum += (s - '0');
}
unordered_map<int, string> mp;
mp[0] = "zero";
mp[1] = "one";
mp[2] = "two";
mp[3] = "three";
mp[4] = "four";
mp[5] = "five";
mp[6] = "six";
mp[7] = "seven";
mp[8] = "eight";
mp[9] = "nine";
string str = to_string(sum);
cout << mp[str[0] - '0'];
int size = str.size();
for (int i = 1; i < size; ++i) {
cout << " " << mp[str[i] - '0'];
}
return 0;
}