编程之美-2.2、寻找n阶乘中末尾0的个数

来源:互联网 发布:20网络用语什么意思 编辑:程序博客网 时间:2024/06/14 22:34
#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;
}
原创粉丝点击