题目描述
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
题目解析
对于这道题目而言,本身并没有太多的难点,只是输入一个数然后各位相加,最后用英文将结果表示出来。只需要注意的时输入的是0的时候怎样输出这样一个特殊的点就ok了。
代码
#include<iostream>
#include<cstdio>
using namespace std;
string intergers[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int main(){
char ch[105];
scanf("%s",&ch);
int sum = 0;
for(int i=0;ch[i]!='\0';i++){
sum += (ch[i]-'0');
}
if(sum==0){cout<<intergers[0]<<endl;return 0;}
string s="";
while(sum!=0){
int x = sum % 10;
if(sum>=10)s = " " + intergers[x] + s;
else s = intergers[x] + s;
sum /= 10;
}
cout<<s<<endl;
}