leetcode172
public class Solution {
public int TrailingZeroes(int n) {
if (n == )
{
return ;
}
else
{
var x = n / ;
var y = TrailingZeroes(x);
return x + y;
}
}
}
https://leetcode.com/problems/factorial-trailing-zeroes/#/description
此类问题很显然属于数学问题,一定要找到其中的本质规律才能得到正确的数学模型。
两个大数字相乘,都可以拆分成多个质数相乘,而质数相乘结果尾数为0的,只可能是2*。如果想到了这一点,那么就可以进一步想到:两个数相乘尾数0的个数其实就是依赖于2和5因子的个数。又因为每两个连续数字就会有一个因子2,个数非常充足,所以此时只需要关心5因子的个数就行了。
对于一个正整数n来说,怎么计算n!中5因子的个数呢?我们可以把5的倍数都挑出来,即:
令n! = (*K) * (*(K-)) * (*(K-)) * ... * * A,其中A就是不含5因子的数相乘结果,n = *K + r(<= r <= )。假设f(n!)是计算阶乘n!尾数0的个数,而g(n!)是计算n!中5因子的个数,那么就会有如下公式:
f(n!) = g(n!) = g(^K * K! * A) = K + g(K!) = K + f(K!),其中K=n / (取整数)。
很显然,当0 <= n <= 4时,f(n!)=。结合这两个公式,就搞定了这个问题了。举几个例子来说: f(!) = + f(!) =
f(!) = + f(!) =
f(!) = + f(!) =
f(!) = + f(!) = + + f(!) =
f(!) = + f(!) = + + f(!) = + + f(!) = + + f() =
以上解释参考地址:https://www.cnblogs.com/kuliuheng/p/4102917.html
python的实现:
class Solution:
def trailingZeroes(self, n: int) -> int:
count =
while (n > ):
count += n //
n = n //
return count
leetcode172的更多相关文章
- LeetCode----172. Factorial Trailing Zeroes(Java)
package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...
- 每天一道LeetCode--172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- [Swift]LeetCode172. 阶乘后的零 | Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- leetcode172 阶乘后的零
对数算法:O(nlogn) /** 即为统计0-n中5,10,15,20,25的个数,因为肯定有足够的偶数使得存在x*5=10*n,25=5*5因此计数加2,5=1*5计数加一: 但如果挨个计数当n很 ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- leetcode探索中级算法
leetcode探索中级答案汇总: https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/ 1)数 ...
随机推荐
- OAF 返回供应商门户主页
if (pageContext.getParameter("returnHomePage") != null) { pageContext.setForceForwardURL(& ...
- OC 数据持久化(数据本地化)- 本地存储
// // ViewController.m // IOS_0113_本地存储 // // Created by ma c on 16/1/13. // Copyright (c) 2016年 博文科 ...
- BZOJ3288 Mato矩阵
网上说高斯消元得到下三角矩阵然后都是phi(i)...反着我是搞不出来 打个表什么的还是能看出来点奇怪的东西,比如后面能整除前面的,然后再乱搞吧2333 /********************** ...
- jquery+html5制作超酷的圆盘时钟表
自己封装的一个用HTML5+jQuery写的时钟表 代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- halcon之屌炸天的变形匹配(1)
在日常工程应用中,我们通常通过halcon的 shape-based matching(形状匹配)进行各种定位, 如以前文章介绍的这样,理解各个参数并灵活应用通常就能得到很好的匹配效果和匹配速度, 当 ...
- 在Google Maps中导出KML文件
教你一招:在Google Maps中导出KML文件 2011-07-11 15:24:49 4819 人阅读 作者:上方文Q 编辑:上方文Q [复制链接] [爆料] Google Earth的一大乐趣 ...
- 在jenkins和sonar中集成jacoco(一)--使用jacoco收集单元测试的覆盖率
之前系统的持续集成覆盖率工具使用的是cobetura,使用的过程中虽然没什么问题,但感觉配置比较麻烦,现在准备改用jacoco这个覆盖率工具来代替它.接下来我介绍一下jenkins配置jacoco,并 ...
- Beta阶段第2周/共2周 Scrum立会报告+燃尽图 08
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2389] 版本控制:https://git.coding.net/liuyy08 ...
- Alpha阶段第1周 Scrum立会报告+燃尽图 06
作业要求与https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246相同 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 周 ...
- windows 下键盘映射
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout下添加二进制键 Scancode Map完后注销后即可生效