阶乘末尾 0 的个数.c

//  阶乘尾数零的个数
//  100!的末尾有多少个0?

/*  
	问题分析和算法设计:
	首先分析在100!结果值的末尾产生0的条件。不难看出:一个整数
	若含有一个因子5则必然会在求100!时产生一个0。因此问题转化为
	求1到100这100个整数中包含了多少个因子5。若整数N能被25整除,
	则N包含2个因子5;若整数N能被5整除,则N包含一个因子5。
*/
#include <stdio.h>

int main()
{
	int count = 0;
	
	int i;
	for (i = 5; i <= 100; i += 5)   // 循环从5开始,以5的倍数为步长
	{
		count++;                    // 若为5的倍数,计数器加1
		if (i % 25 == 0)
			count++;                // 若为25的倍数,计数器再加1
	}
	
	printf ("The number of 0 in the end of 100! is:%d.\n", count);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40383812/article/details/84567405
今日推荐