Lintcode题目计算n阶乘中尾部0的个数

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 18:53   1026   0


用10!这个数字做思考,发现每五个数得到一个0,总结规律

z=n/5+n/(5*5)+n/(5*5*5)+…+(直到n小于n的a次幂) //来源公式之美

代码如下:

class Solution {
public:
/*
* @param n: A long integer
* @return: An integer, denote the number of trailing zeros in n!
*/
long long trailingZeros(long long n) {
// write your code here, try to do it without arithmetic operators.
long num = 0;
while(n > 0) {
num += n / 5;
n /= 5;
}
return num;
}
};


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP