(pta基础编程题目)6-9 统计个位数字 (15 分)

版权声明:©2018-2020 Macromedia, Inc. All rights reserved. 文章为时间的荒野所原创如需转载请联系博主。 https://blog.csdn.net/qq_43444398/article/details/89007055
本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。
函数接口定义:
int Count_Digit ( const int N, const int D );

其中N和D都是用户传入的参数。N的值不超过int的范围;D是[0, 9]区间内的个位数。函数须返回N中D出现的次数。

裁判测试程序样例:
#include <stdio.h>

int Count_Digit ( const int N, const int D );

int main()
{
int N, D;

scanf("%d %d", &N, &D);
printf("%d\n", Count_Digit(N, D));
return 0;

}

/* 你的代码将被嵌在这里 */
输入样例:
-21252 2
输出样例:
3

int Count_Digit ( const int N, const int D )
{
	if(N==0)//为0 输出为1
	{
		return 1;
	}
	int m=0,z=0,g=0,n,d;
	n=N;
	d=D;
	if(n<0)//取绝对值
	{
		n=(-1)*n;
	}
	for(int i=0;n!=0;i++)
	{
		if(n%10==D)
		{
			g++;//计算个数
		}
		n=n/10;//每次减少一位。
	}
	return g;
}

自律生活第二十二天。继续坚持每天半个小时练字,半小时学习吉他。

希望自己自律上瘾。

我不是在简单的编程,而是在创造一个“世界”。

江客:时荒

猜你喜欢

转载自blog.csdn.net/qq_43444398/article/details/89007055