1005 Spell It Right (20 分)
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 (≤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
输入:一个非负整数 n
输出:n的各个位数相加后得到sum 用英文输出sum的各个位数
使用字符串和数转换 更简单,
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string str[10] =
{"zero","one","two","three","four","five","six","seven","eight","nine"};
string s;
cin >> s;
int sum = 0;
for(int i = 0;i < s.length();i++){
sum += s[i] - '0';
}
string arr = to_string(sum);
cout << str[arr[0]-'0'];
for(int i = 1;i < a.length();i++)
cout << " " << str[a[i]-'0'];
return 0;
}