`
java-mans
  • 浏览: 11454676 次
文章分类
社区版块
存档分类
最新评论

N!末尾有多少个0

 
阅读更多

其实就是找1,2,3,4,5...25总共可以分解为多少个5

intFindFive(intn)
{
intresult=0;
for(inti=1;i<=n;i++)
{
if(i%5==0)
{
result++;
}
}
returnresult;

}
这个是错误的,因为25其实相乘的话那么就贡献了2个5.
下面是正确的:
// FindZero.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
int FindFive(int n)
{
 int result=0;
 int j=0;
 for (int i=1;i<=n;i++)
 {
  j=i;
  while (j%5==0)
  {
   result++;
   j/=5;
   
  }
 }
 return result;

}

int _tmain(int argc, _TCHAR* argv[])
{
 int result=FindFive(25);
 printf("%d\n",result);
 system("pause");
 return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics