1005 Spell It Right

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

running code:

#include<cstdio>
#include<cstring>
char English[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"}; 
int main()
{
    char N[101];
    scanf("%s",N);
    int len=strlen(N),sum=0;
    for(int i=0;i<len;i++)
    {
      sum+=(N[i]-'0');	
	}
	int res[10],reslen=0;
	if(sum==0) printf("%s",English[0]);
	while(sum>0)
	{
		res[reslen]=sum%10;
		sum/=10;
		reslen++; 
	}
	for(int j=reslen-1;j>=0;j--)
	{
	    printf("%s",English[res[j]]);
		if(j>0) printf(" ");
	 } 
    return 0;
}

result:

PAT (Advanced Level) Practice 1005 Spell It Right

相关文章: