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

C语言——PAT 乙级(1002.读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。)

更新方法:通过!
思想:
1.字符串操作:简言之,将用户输入的数字当做字符串处理,将各个位数之和的total也当做字符串处理(用到了数字转换为字符串)
2.简单的GetPlace()子函数求数字的位数,确定循环的次数

下面是如何将数字转换为字符串的方法:(total是int)

char s[100];
sprintf(s,"%d",total);   //将最终数值转换成字符串

上代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int GetPlace(int n);
int main()
{
	int i,total,p;
	char c;
	c=getchar();
	i=0;
	total=0;
	while (c!='\n' && c!='\0')
	{
		switch(c)
		{
			case '1':
				total+=1;
				break;
			case '2':
				total+=2;
				break;
			case '3':
				total+=3;
				break;
			case '4':
				total+=4;
				break;
			case '5':
				total+=5;
				break;
			case '6':
				total+=6;
				break;	
			case '7':
				total+=7;
				break;
			case '8':
				total+=8;
				break;
			case '9':
				total+=9;
				break;
			case '0':
				total+=0;
				break;	
		}
		i++;
		c=getchar();
	}
	char s[100];
	sprintf(s,"%d",total);   //将最终数值转换成字符串
	
	i=0;
	p=GetPlace(total);
	
	while (s[i]!='\0')
	{
		switch(s[i])
		{
			case '1':
				printf("yi");
				break;
			case '2':
				printf("er");
				break;
			case '3':
				printf("san");
				break;
			case '4':
				printf("si");
				break;
			case '5':
				printf("wu");
				break;
			case '6':
				printf("liu");
				break;
			case '7':
				printf("qi");
				break;
			case '8':
				printf("ba");
				break;
			case '9':
				printf("jiu");
				break;
			case '0':
				printf("ling");
				break;
		}
		if (i<p-1)
		{
			printf(" ");
		}
		else
		{
			break;
		}
		i++;
	}
	return 0;
}
int GetPlace(int n) 
{
	int t=1;
	int place=1;
	while ((n/t)>=10)
	{
		t*=10;
		place++;
	}
	return (place);
}

在这里插入图片描述

以下是原内容:
不知道为什么,总是过不了!!但是自己电脑上就可以过
最笨的方法:没有借助于指针,字符串,数字,纯数学方法:
只用了一个子函数:GetPlace()用于获取数字的位数

#include <stdio.h>
#include <math.h>
int GetPlace(int n);
int main()
{
	int num;           
	(void)scanf("%d",&num);
	int p;        
	p=GetPlace(num);
	
	int total,i;
	total=0;
	         
	for (i=p-1;i>=0;i--)
	{
		int k;
		k=pow(10,i);

		total+=num/k;

		num-=num/k*k;
	} 

	int a;
	a=GetPlace(total);  
	
	for (i=a-1;i>=0;i--)
	{
		int sign;
		
		int k;
		k=pow(10,i);
	
		sign=total/k;
	
		total-=total/k*k;
		
		switch(sign)
		{
			case 0:
				printf("ling");
				break;
			case 1:
				printf("yi");
				break;
			case 2:
				printf("er");
				break;
			case 3:
				printf("san");
				break;
			case 4:
				printf("si");
				break;
			case 5:
				printf("wu");
				break;
			case 6:
				printf("liu");
				break;
			case 7:
				printf("qi");
				break;
			case 8:
				printf("ba");
				break;
			case 9:
				printf("jiu");
				break;
		}
		if (i!=0)
		{
			printf(" ");
		}
		if (i==0)
		{
			break;
		}
	 } 
	return 0;
}
int GetPlace(int n) 
{
	int t=1;
	int place=1;
	while ((n/t)>=10)
	{
		t*=10;
		place++;
	}
	return (place);
}

这是电脑上的运行结果:
正常的运行结果
但是题目的示例就运行异常:
在这里插入图片描述
运行追踪了一下,发现异常的原因是因为这个数获取的位数是10
在这里插入图片描述
PAT网站运行结果:
在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值