zoukankan      html  css  js  c++  java
  • 172. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!.

    Example 1:

    Input: 3
    Output: 0
    Explanation: 3! = 6, no trailing zero.

    Example 2:

    Input: 5
    Output: 1
    Explanation: 5! = 120, one trailing zero.
    class Solution {
        public int trailingZeroes(int n) {
        // return n==0 ? 0 : n/5 + trailingZeroes(n/5);
            int res=0;
            while(n>0){
                res += n/5;
                n/=5;
            }
            return res;
        }
    }

    看见没有,一行就能搞定??我感觉我像个弱智

    例1

    n=15。那么在15! 中 有35(来自其中的5, 10, 15), 所以计算n/5就可以。

    例2

    n=25。与例1相同,计算n/5,可以得到55,分别来自其中的5, 10, 15, 20, 25,但是在25中其实是包含25的,这一点需要注意。

    所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, ..., n/5/5/5,,,/5直到商为0,然后就和,就是最后的结果。



    作者:ab409
    链接:https://www.jianshu.com/p/211618afc695
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    Ionic Android开发环境搭建 下
    Ionic Android开发环境搭建 上
    百度地图API 简单使用
    json2.js 的使用
    第三回 Bootstrap3.x 起步
    第二回 认识CDN
    WPF 实现的等待效果界面
    AutoFac使用
    SQL语句优化
    MySQL索引的使用
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10653848.html
Copyright © 2011-2022 走看看