C语言求阶乘尾数零的个数

版权声明:小简原创 https://blog.csdn.net/qq_43469554/article/details/84851429

题目:100!的尾数有多少个零?
算法分析:
不难发现,只要一个整数含有一个5因子,那么就必然后会产生一个0,那么就只要考虑1 ~ 100中5的倍数的数了。利用这个规律,题目很容易解决了。

代码:

#include<stdio.h>
int main()
{
	int a;
	int count;
	count =0;
	for(a=5; a<=100; a+=5) //循环从5开始,以5的倍数为步长,考察整数
	{
		count++; //若为5的倍数,计数器加1
		if(!(a%25))
		{
			count++; //若为25的倍数,计数器再加1
		}
	printf("%d",count); //打印结果
	return 0; 
}

输出示例:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/84851429