A1005 Spell It Right

A1005 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

Code:

#pragma warning(disable: 4996)
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]) {
int sum = 0;
//while (scanf("%1d", &digit) != EOF)不好使
char buffer[101];
scanf("%s", buffer);
for (int i = 0; i < strlen(buffer); i++)
sum += buffer[i] - '0';
char num[10][7] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
sprintf(buffer, "%d", sum);
printf("%s", num[buffer[0] - '0']);
for (int i = 1; i < strlen(buffer); i++)
printf(" %s", num[buffer[i] - '0']);
/*if (i != strlen(buffer) - 1)
printf(" ");这样判断会慢一点*/
system("pause");
return 0;
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×