#include "stdafx.h"
//方法一、
int count_01(int n)
{
int ret=0;
int i,j;
for(i=1;i<=n;i++)
{
j=i;
while(j%5==0)
{
ret++;
j/=5;
}
}
return ret;
}
//方法二、
int count_02(int n)
{
int ret=0;
while(n)
{
ret+=n/5;
n/=5;
}
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
int count;
scanf("%d",&n);
count=count_02(n);
printf("n阶乘末尾0的个数: %d\n",count);
return 0;
}
//方法一、
int count_01(int n)
{
int ret=0;
int i,j;
for(i=1;i<=n;i++)
{
j=i;
while(j%5==0)
{
ret++;
j/=5;
}
}
return ret;
}
//方法二、
int count_02(int n)
{
int ret=0;
while(n)
{
ret+=n/5;
n/=5;
}
return ret;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n;
int count;
scanf("%d",&n);
count=count_02(n);
printf("n阶乘末尾0的个数: %d\n",count);
return 0;
}
本文提供两种计算n阶乘末尾0的个数的方法。方法一采用循环遍历并计数的方式,方法二则使用更高效的迭代算法,通过不断除以5累加商来快速得出结果。

被折叠的 条评论
为什么被折叠?



