Not Only Algorithm,不仅仅是算法,关注数学、算法、数据结构、程序员笔试面试以及一切涉及计算机编程之美的内容 。。
你的位置:NoAlGo博客 » 题解 » 

PAT 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:


Sample Output:

one five


#include <cstdio>

int main()
	char num[105]; scanf("%s", num);

	int sum = 0;
	for (int i = 0; num[i]; i++) sum += num[i] - '0';

	char s[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

	char ssum[5]; sprintf(ssum, "%d", sum);

	printf("%s", s[ssum[0]-'0']);
	for (int i = 1; ssum[i]; i++)
		printf(" %s", s[ssum[i]-'0']);

	return 0;
上一篇: 下一篇: