Trailing Zeros

原创 2018年04月16日 13:57:58

Write an algorithm which computes the number of trailing zeros in n factorial.

Example

11! = 39916800, so the out should be 2

Related Problems 

一开始没想明白,后来随手列了一些相乘尾数能等于0的数,2*5,4*5,5*6,5*8。发现都是与5相关。所以其实就是求5的数量

class Solution {
public:
    /*
     * @param n: A long integer
     * @return: An integer, denote the number of trailing zeros in n!
     */
    long long trailingZeros(long long n) {
        // write your code here, try to do it without arithmetic operators.
      long long sum=0;
      while(n){
          sum+=n/5;
          n=n/5;
      }
      return sum;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。如果您有更好的想法,请留言 https://blog.csdn.net/qq_34552886/article/details/79959681

2. Trailing Zeros

class Solution { /* * param n: As desciption * return: An integer, denote the number of t...
  • Johanan_luo
  • Johanan_luo
  • 2018-03-25 00:52:47
  • 5

[LeetCode] 零的个数 number of trailing zeros

给定一个整数 n,求出n的阶乘的尾零的个数。 思路:只有2和5相乘才能得到一个尾零。如果2^2和5^2相乘,能得到两个尾零。。。 代码: int numZeros(int num) { int ...
  • jiyanfeng1
  • jiyanfeng1
  • 2015-07-31 12:30:33
  • 690

lintcode:Trailing Zeros

15:00 Start Write an algorithm which computes the number of trailing zeros in n factorial. ...
  • gao1440156051
  • gao1440156051
  • 2016-01-30 23:09:57
  • 546

Factorial Trailing Zeros

leetcode question Given an integer n, return the number of trailing zeroes in n!.Your solution shoul...
  • xjwun
  • xjwun
  • 2015-01-13 08:14:26
  • 1133

Codewars --Number of trailing zeros of N!

Codewars上做过的一个题
  • Armour__r
  • Armour__r
  • 2017-02-11 20:39:42
  • 215

LeetCode Factorial Trailing Zeroes数学方法详解

Given an integer n, return the number of trailing zeroes in n!.题目的意思是要求一个整数的阶乘末尾有多少个0;1.需要注意的是后缀0是由2...
  • taoyanqi8932
  • taoyanqi8932
  • 2016-08-05 15:14:24
  • 1300

Leetcode: Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in lo...
  • doc_sgl
  • doc_sgl
  • 2015-01-02 21:23:32
  • 7171

Factorial Trailing Zeroes Produced By Java

题目来自LeetcodeQJ Given an integer n, return the number of trailing zeroes in n!. Note: Your solution...
  • u012807459
  • u012807459
  • 2015-02-04 19:42:55
  • 355

02 Number of trailing zeros of N!(求N的阶乘末尾0的数目)

题目描述Write a program that will calculate the number of trailing zeros in a factorial of a given num...
  • kejingli2015
  • kejingli2015
  • 2016-11-19 17:03:06
  • 144
收藏助手
不良信息举报
您举报文章:Trailing Zeros
举报原因:
原因补充:

(最多只允许输入30个字)