数字的末尾为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 = ;
while(n){
count += n/;
n = n/;
}
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。

leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)的更多相关文章

  1. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

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

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

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

  3. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

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

  4. 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 ...

  5. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

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

  6. Java [Leetcode 172]Factorial Trailing Zeroes

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

  7. Java for LeetCode 172 Factorial Trailing Zeroes

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

  8. 172 Factorial Trailing Zeroes 阶乘后的零

    给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...

  9. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

随机推荐

  1. appium 自动化测试环境搭建

    最近再学习appium,把学习的过程记录下来,以防止到时候 换个电脑就不知道这么安装搭建appium环境了. 环境搭建: 0.JDK环境是必备的,这里大家自行百度,   1.安装 node 环境,前辈 ...

  2. 为什么angular library的build不能将assets静态资源打包进去(转)

    Versions Angular CLI: 6.0.7 Node: 9.3.0 OS: darwin x64 Angular: 6.0.3 ... animations, common, compil ...

  3. 安全专家发现GE Multilin SR的一个关键漏洞对全球电网构成严重威胁。

    A team of researchers from New York University has found a serious vulnerability in some of GE Multi ...

  4. zookeeper+activemq高可用集群搭建

    一.准备工作: 准备三台机器:192.168.35.111192.168.35.112192.168.35.113 二.搭建zookeeper 三台机器上均要搭建zookeeper服务// 下载zoo ...

  5. Codeforces Round #344 (Div. 2) D. Messenger (KMP)

    D. Messenger time limit per test2 seconds memory limit per test512 megabytes inputstandard input out ...

  6. 使用SpringBoot做Javaweb时,数据交互遇到的问题

    有段时间没做过javaweb了,有点生疏了,js也忘记得差不多,所以今天下午做前后端交互的时候,传到后台的参数总是为空,前端控制台了报一个String parameter “xxx” is not p ...

  7. inode、软硬链接

    关于inode是什么,可以看这篇文章:http://www.cnblogs.com/adforce/p/3522433.html 如何查看inode ll -di /boot / /app查看文件和文 ...

  8. repo 回退当前分支下所有仓库到指定日期前的最新代码版本

    回退命令: repo forall -c 'commitID=git log --before "2019-11-24 23:59" -1 --pretty=format:&quo ...

  9. ubuntu桌面最大化

    三行命令搞定Ubuntu 16.04下安装VMware Tools!!!!!!!!! 由于下载的是ubuntu-16.04.3-desktop-amd64,需要安装vmware tools,以往提取的 ...

  10. win7系统安装虚拟网卡进行与Linux虚拟机的通信

    有网络情况下,安装Linux时选择网桥即可实现Window与Linux直接通信. 无网络情况下,最简单的方法是在window系统中安装虚拟网卡,以进行与Linux的通信,步骤如下: (1)右击“我的计 ...