zoukankan      html  css  js  c++  java
  • leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)

     数字的末尾为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。
  • 相关阅读:
    定制事件 观察者模式
    定时器的高级运用 优化
    tamper-proof 对象 nonextensible对象 sealed对象 frozen对象
    函数柯理化
    跨域 Ajax 其他可选技术 异步
    Ajax 跨域 异步 CORS
    原样输出html标签
    JavaScript
    css 中name的用途
    iview 按需引入解决加载慢的问题
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/9652147.html
Copyright © 2011-2022 走看看