PAT-A 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 (<= 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<stdio.h>
#include<string.h>
#define MAX 101
char c[MAX];
char ans[MAX]={0};
char set[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
void num_to_char(int n);
int main()
{
scanf("%s",c);
int len = strlen(c),i,sum = 0;
for(i=0;i<len;i++)
{
sum = sum + c[i]-'0';
}
num_to_char(sum);
len = strlen(ans);
for(i=len-1;i>=0;i--)
{
printf("%s",set[ans[i]-'0']);
if(i!=0)
putchar(' ');
}
return 0;
}
void num_to_char(int n)
{
if(n==0)
{
ans[0] = '0';
ans[1]= '\0';
return;
}
int i=0;
while(n!=0)
{
ans[i] = n%10 + '0';
i++;
n=n/10;
}
ans[i] = '\0';
return;
}