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:

12345

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']);
	printf("\n");

	return 0;
}
上一篇: 下一篇:

我的博客

NoAlGo头像编程这件小事牵扯到太多的知识,很容易知其然而不知其所以然,但真正了不起的程序员对自己程序的每一个字节都了如指掌,要立足基础理论,努力提升自我的专业修养。

站内搜索

最新评论