problem

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 (≤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各位数的和,并输出英文读音

solution

  • 求出和sum后,可以用函数将sum转换为string并直接输出读音
  • 也可以递归到底逐位输出读音。
#include<iostream>
#include<string>
using namespace std;
int main(){
string s; cin>>s;
int sum = 0;
for(int i = 0; i < s.size(); i++)
sum += s[i]-'0';
string ans = to_string(sum);
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cout<<arr[ans[0]-'0'];
for(int i = 1; i < ans.size(); i++)
cout<<" "<<arr[ans[i]-'0'];
return 0;
}