『质因数分解』题解 | #阶乘末尾0的数量#
阶乘末尾0的数量
http://www.nowcoder.com/practice/aa03dff18376454c9d2e359163bf44b8
01.超时解法
- 但是方法是对的
class Solution {
public:
/**
* the number of 0
* @param n long长整型 the number
* @return long长整型
*/
long long thenumberof0(long long n) {
// write code here
long long res=0;
for(long long loop=5; loop<=n; loop+=5 )
{
long long temp=loop;
while( 0==temp%5 )
{
++res;
temp/=5;
}
}
return res;
}
}; - 测试如下
运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。 6/11 组用例通过 运行时间 2001ms 占用内存 428KB
02.数学公式
class Solution {
public:
/**
* the number of 0
* @param n long长整型 the number
* @return long长整型
*/
long long thenumberof0(long long n) {
// write code here
long long res=0;
while( n )
{
res+=( n/5 );
n/=5;
}
return res;
}
}; 