1005. Spell It Right (20)

http://www.patest.cn/contests/pat-a-practise/1005


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>
#define bool int
#define false 0
#define true 1
int main(){
	char *num2char[] = {"zero","one","two","three","four",
	"five","six","seven","eight","nine"};
	bool first = true;
	char chs[200]; //字符数组保存输入的数据(数据太大时long long貌似都不够)
	int A[200];//保存各位之和的每一位数字
	int sum,n;//sum-各位之和,n-暂存和的每一位
	char ch;//用于输入chs时暂存字符
	int i = 0,cnt;
	while( '\n' != (ch = getchar())){//输入数据
		chs[i++] = ch;
	}
	cnt = i;
	if(i == 1 && chs[0]=='0'){//输入只有一个0时
		printf("zero");
		return 0;
	}
	sum = 0;
	for(i = 0;i < cnt;++i){//计算各位之和
		sum =  sum + (chs[i] + ( 0 - '0'));
	}
	cnt = 0;
	while(sum){//取和sum的各位保存到A[]
		n = sum % 10;
		sum = sum / 10;
		A[cnt++] = n;
	}
	cnt--;
	for(i = cnt;i >= 0 ;--i){
		if(first) first = false;//第一次打印时不打印空格
		else printf(" ");
		printf("%s",num2char[A[i]]);
	}
	return 0;
}


你可能感兴趣的:(算法,解题报告,pat,1005)