数字的末尾为0实际上就是乘以了10,20、30、40其实本质上都是10,只不过是10的倍数。10只能通过2*5来获得,但是2的个数众多,用作判断不准确。

以20的阶乘为例子,造成末尾为0的数字其实就是5、10、15、20。

多次循环的n,其实是使用了多个5的数字,比如25,125等等。

n/5代表的是有多个少含5的数,所以不是count++,而是count += n/5

class Solution {
public:int trailingZeroes(int n) {int count = 0;while(n){count += n/5;n = n/5;}return count;}
};

https://blog.csdn.net/feliciafay/article/details/42336835

  • //计算包含的2和5组成的pair的个数就可以了,一开始想错了,还算了包含的10的个数。
  • //因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定。
  • //观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以。
  • //但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25=(5*5)的另外一个5),
  • //所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, ..., n/5/5/5,,,/5直到商为0。

转载于:https://www.cnblogs.com/ymjyqsx/p/9652147.html

leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)相关推荐

  1. LeetCode 172. Factorial Trailing Zeroes

    LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...

  2. Leetcode 172 Factorial Trailing Zeroes

    1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  3. 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码

    文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...

  4. LeetCode(172) Factorial Trailing Zeroes

    题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  5. 172. Factorial Trailing Zeroes

    /**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...

  6. 【leetcode❤python】172. Factorial Trailing Zeroes

    #-*- coding: UTF-8 -*- #给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0. #所有的尾部的0可以看做都是2*5得来的, ...

  7. leetcode python3 简单题172. Factorial Trailing Zeroes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...

  8. 【LeetCode】172 - Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  9. Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]

    题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

最新文章

  1. 听说你想从事中间件开发?
  2. linux epoll 文件,Linux面试必知:一句话讲透epoll-文件句柄
  3. 8. An Introduction to MCMC for Machine Learning (3)
  4. 牛客 - Across the Firewall(最大流)
  5. POJ1177矩形面积并(矩形切割+括号匹配)
  6. CSDN开放90%页面缩放阅读博文的惊喜
  7. 29.课时29.【Django模板】url标签使用详解(Av61533158,P29)
  8. java indexof 通配符,字符串与含有通配符‘*’的字符串匹配(非正则表达式)
  9. 化工计算机软件基础考试题,2013年化工工程师基础知识网友版真题(下午卷)...
  10. Python图像纹理分割
  11. 基于.net平台的Windows窗体应用程序——(已附源码)学生管理系统
  12. 从零开始学Android应用安全测试
  13. 【C++】字体文件解析(FreeType)
  14. SIFT与SURF算法
  15. 计算机科学与技术专业宣传口号,十大经典深入人心科技类广告语
  16. 1138: C语言合法标识符
  17. 【简单利用函数实现多条件求和】
  18. 为什么ppt图形卡配置不正确_PPT常见问题解决方法,PPT检测到图形卡可能配置不正确怎么办?...
  19. Tomcat多实例与负载均衡
  20. c++椭圆最小二乘法原理_利用最小二乘法拟合椭圆方程的理论推导,附有matlab代码...

热门文章

  1. 用easyx画五角星_冷军画了一把破剪刀, 竟然卖到了160万,被专家抨击:乱涂鸦!得奖后专家顿时不说话了...
  2. 美赛整理之遗传算法优化BP神经网络的齿轮故障诊断问题
  3. github 怎么搜索_实用!8个玩转GitHub的小插件,快来试试吧!
  4. c++ 三角函数_精准备考 | 初中数学三角函数知识点全归纳
  5. java闭包lambda,(lambda)函数闭包捕获了什么?
  6. monggodb和MySQL同步问题_monggoDB添加到windows服务
  7. Hive压缩存储(以Parquet为例)
  8. DataNode 运行状况
  9. UML类图关系全面剖析[转]
  10. 绕开“陷阱“,阿里专家带你深入理解C++对象模型的特殊之处