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
思路
很幸运的是,我随便挑了一题甲级的来刷,结果居然发现和上一次做的乙级的题目几乎一模一样(乙级1002 写出这个数(20分)),这题同样是计算给出字符串的每一位数的总和,只不过甲级这道题要求的是英文输出,乙级的是中文拼音输出。
思路当然还是同样的,先把输入字符串的每一位加起来,然后再把总和sum转化为字符串result,最后对result的每一位取对应的英文就行啦~
代码
#include<string>
#include<cstdio>
#include<iostream>
using namespace std;
char number[10][11]={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
string tmp;
cin>>tmp;
int sum = 0;
for(int i=0;i<tmp.length();i++){
sum += tmp[i]-'0';
}
string result = to_string(sum);
for(int i=0;i<result.length();i++){
if(i==0) cout<<number[result[i]-'0'];
else cout<<" "<<number[result[i]-'0'];
}
return 0;
}