1005 Spell It Right (20 分)
Given a non-negative integer NNN, your task is to compute the sum of all the digits of NNN, 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 NNN (≤10100\le 10^{100}≤10100).
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
思路
字符串的处理,先把所有的数字加起来求和,然后转成字符串,通过映射输出每个字符串的英文字母。水题。。。
代码
#include <iostream>
#include <string>
using namespace std;
int main() {
string s, smap[10] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cin >> s;
long long int sum = 0;
for (int i = 0; i < s.length(); i++) {
sum += s[i] - '0';
}
s = to_string(sum);
for (int i = 0; i < s.length(); i++) {
if (i != 0) cout << " ";
cout << smap[s[i] - '0'];
}
return 0;
}