PATA1005Spell It Right

  • 考虑输入为0的特殊情况
    参考代码:
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cstring>
#include<cstdlib>
char N[110];
char num[10][6] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int main()
{
	int len = 0;
	int sum = 0;//用来存放每一位的数字和
	int ans[1000] = {0};//用来存放sum的每一位数字,以便输出
	scanf("%s", N);
	len = strlen(N);
	for (int i = 0; i < len; i++)
	{
		sum = sum + (N[i] - '0');
	}
	int k = 0;
	if (sum == 0) printf("zero");
	while (sum)
	{
		ans[k] = sum % 10;
		sum /= 10;
		k++;
	}

	for (int i = k - 1; i >= 0; i--)
	{
		printf("%s", num[ans[i]]);
		if (i != 0) printf(" ");
	}

	system("pause");
	return 0;
}
posted @ 2019-07-23 18:05  睿晞  阅读(143)  评论(0编辑  收藏  举报