1005. Spell It Right (20)

来源:互联网 发布:cf手游安卓版飞天软件 编辑:程序博客网 时间:2024/05/17 07:44

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 1int 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;}


0 0