PAT考试乙级1002之写出这个数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/a029019/article/details/100572635

写出这个数

题目:

读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:
每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 10
​100
​​ 。

输出格式:
在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:
1234567890987654321123456789
输出样例:
yi san wu

代码:

#include <stdio.h>
#include <string.h>
void shuchu(int a)
{
	if(a == 0)
	{
		printf("ling");
	}
	if(a == 1)
	{
		printf("yi");
	}
	if(a == 2)
	{
		printf("er");
	}	
	if(a == 3)
	{
		printf("san");
	}	
	if(a == 4)
	{
		printf("si");
	}	
	if(a == 5)
	{
		printf("wu");
	}	
	if(a == 6)
	{
		printf("liu");
	}
	if(a == 7)
	{
		printf("qi");
	}	
	if(a == 8)
	{
		printf("ba");
	}	
	if(a == 9)
	{
		printf("jiu");
	}
}
int main(void)
{
	char array[101];
	int i, n = 1, total = 0, q =1, len, a, to = 0;
	scanf("%s",array);
	len = strlen(array);
	for(i = 0; i <len; i++)
	{
		total = total + (array[i]-48);
	}
	to = total;
	while(to / 10 != 0)
	{
		to = to / 10;
		n++;
	}
	for(i = 0; i < n-1; i++)
	{
		q = q*10;
	}
	for(i = 0; i < n; i++)
	{
		if(total <10)
		{
			shuchu(total);
		}
		if(total >= 10)
		{
			a = total / q;
			shuchu(a);
			printf(" ");
			total = total % q;
			q = q / 10;
			if(q == 10)
			{
				if(total <10)
				{
					shuchu(0);
					printf(" ");
					q = q/10;
					i++;
				}
			}
			
		}
	}
	return 0;
}

这道题我做的复杂了很多,实际上可以把拼音放进一个数组,最后用数组输出就可以的,这样比较简洁。
这道题要注意的是:最后算出来的值不超过三位数,当有三位数时,要考虑十位为0的情况,可能没注意就会拆不出来0。

猜你喜欢

转载自blog.csdn.net/a029019/article/details/100572635