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
题意简单明了,就是求给定数的各位和,然后再将和的各位分开用英语数字输出。
参考代码:
#include <cstdio>
#include <cstring>
char num[10][6]={"zero","one","two","three","four","five","six","seven","eight","nine"};
char n[110];
int main()
{
while(scanf("%s",n)!=EOF)
{
int sum=0,count=0;
int ans[100]={0};
int len=strlen(n);
for(int i=0;i<len;++i)
{
sum+=n[i]-'0'; //求每位和
}
do{
ans[count++]=sum%10;//计算和的每位
sum/=10;
}while(sum);
for(int i=count-1;i>0;--i)
{
printf("%s ",num[ans[i]]);
}
printf("%s\n",num[ans[0]]);
}
return 0;
}