zoukankan      html  css  js  c++  java
  • leetcode 172

    172. Factorial Trailing Zeroes

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

    Note: Your solution should be in logarithmic time complexity.

    计算出n!中尾部0的个数。1*2*3*......*n中0的个数由2和5的对数决定,由于因子5的个数小于因子2的个数,所以只需求出因子5 的个数即为尾部0的个数。

    n/5可以求得能被5整除数的个数;

    但25中存在两个因子,125中存在三个因子,依次类推。

    所以在求因子5的时候应该考虑到。

    代码如下:

     1 class Solution {
     2 public:
     3     int trailingZeroes(int n) {
     4         int count = 0;
     5         while(n)
     6         {
     7             count += n/5;
     8             n /= 5;
     9         }
    10         return count;
    11     }
    12 };
  • 相关阅读:
    移动布局---1. 移动端布局基础
    1. CSS新特性之选择器
    1. H5新增语义化标签
    POJ 3281
    poj 1986
    POJ 3728
    poj 2763
    poj 2749
    uva 11294
    LA 3713
  • 原文地址:https://www.cnblogs.com/shellfishsplace/p/6043585.html
Copyright © 2011-2022 走看看