## PAT一道十分简单的题的疑问

schecter尧 发布于 2017/02/22 19:59

PAT的甲级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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 200

int main()
{
char num[N], s[N], *p = num;
int n[N];
int sum = 0, i, t = 0;
gets(num);
for (i = 0; i<strlen(num); p++, i++)
{
sscanf(p, "%1d", &n[i]);
sum += n[i];
}
sprintf(s, "%d", sum);
for (i = 0; i<strlen(s); i++)
{
switch (s[i])
{
case '0':printf("zero "); break;
case '1':printf("one "); break;
case '2':printf("two "); break;
case '3':printf("three "); break;
case '4':printf("four "); break;
case '5':printf("five "); break;
case '6':printf("six "); break;
case '7':printf("seven "); break;
case '8':printf("eight "); break;
case '9':printf("nine "); break;
default:
break;
}
}
printf("\b\n");
system("pause");
return 0;
}

0

but no extra space at the end of a line.

s

s