您的位置:首页 > 其它

1005. Spell It Right (20)

2018-03-01 17:09 344 查看
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
我有罪呀,我忘记字符串怎么初始化呀,特别是指针式字符串,例如char *input, 我怎么指定这个字符串大小呀?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char *nToW[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

char input[101];
/**< 读入部分 */
gets(input);
int len = strlen(input);

/**< 计算各个位数相加结果 */
int sum = 0;
int i;
for(i=0; i<len; i++)
{
sum += (input[i]-'0');
}

/**< 计算sum最大能被哪个数除还能剩下个位数 */
int divider = 1;
while(1)
{
if(sum/divider<10)
{
break;
}
else
{
divider *= 10;
}
}
/**< 逐个计算商并输出 */
int quotient;
while(divider>0)
{
quotient = sum/divider;
sum %= divider;
printf("%s", nToW[quotient]);
if(divider!=1)
printf(" ");
divider /= 10;
}

system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: