pat

题目

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

注意:
(1)特判0的情况。
(2)注意$N$的取值范围,应该用string字符串类型来存储。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <string>
#include <vector>
using namespace std;
string str[10] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
int (){
int sum = 0;
string N;
cin >> N;
if(N == "0"){
printf("zero");
}else{
for(int i = 0;i < N.size();i++){
sum += (N[i] - '0');
}
vector<int> V;
while(sum){
int tmp = sum % 10;
sum /= 10;
V.push_back(tmp);
}
bool first = true;
for(auto iter = V.rbegin(); iter != V.rend(); iter++){
if(first){
cout << str[iter];
first = false;
}else cout << " " << str[
iter];

}
}
return 0;
}