class Solution {
public:

int trailingZeroes(int n) {

           if(n<=0) return 0;

int i=0;
           int res=0;

while(n){
      res+=n/5;
      n=n/5;
     }
return res;
}
};

很神奇的,eg  125

125=25*5,相当于前前面有1,2,3,4,5,……,20,21,22,23,24,25  * 5

125/5 = 25 相当于前面变成0,0,0,0,1,……,4,4,4,4,4,5               *5

25/5=5  所以最后结果结果过就是25+5+1

n阶乘 尾数0的个数的更多相关文章

  1. Algorithm --> 求阶乘末尾0的个数

    求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...

  2. Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)

    一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...

  3. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

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

  4. N的阶乘末尾0的个数和其二进制表示中最后位1的位置

    问题一解法:     我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个 ...

  5. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

  6. nyoj 84阶乘后0的个数

    描述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示测试数据的组数(1<=N<=100)每组测试数据占一行,都只有一个整数M(0<=M<=10000000) ...

  7. 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  8. [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数

    所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...

  9. 计算阶乘n!末尾0的个数

    一.问题描述 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数.例如: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= ...

随机推荐

  1. Objective-C中 Self和 Super详解

    Objective-C中 Self和 Super详解 Objective-C 中Self 和 Super 详解本文要介绍的内容,在 Objective-C 中的类实现中经常看到这两个关键字 self  ...

  2. ViewFlipper、ViewPager和Gallery

    1.ViewFlipper 1)View切换的控件—ViewFlipper介绍 ViewFilpper类继承于ViewAnimator类.而ViewAnimator类继承于FrameLayout. 查 ...

  3. MongoDB的C#封装类

    代码: samus驱动 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  4. 前端实战——照片墙gallery的实现

    对应的html代码 <!doctype html> <html lang="zh-hans"> <head> <meta charset= ...

  5. 连接sql server数据库的两种方式

    class DB     { private static SqlConnection conn; public static SqlConnection getConn() { //conn = n ...

  6. 常用<meta>标签

    页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...

  7. weka特征选择(IG、chi-square)

    一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...

  8. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  9. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  10. int和long long有符号整形 负数比正数多一个

    int的负数比正数多一个,则有一个负数在int范围内没有对应的正数 最大正整数用十六进制,很容易表示:0x7f ff ff ff int num = 0x7fffffff; num = -num; p ...