数字的末尾为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. SVM处理多分类问题

    "one-against-one" approach from sklearn import svm X = [[0], [1], [2], [3]] Y = [0, 1, 2, ...

  2. Docker容器入门之一:部署SpringBoot项目

    一.环境准备:    1.vm虚拟机: Workstation 12 Pro 12.5.7 build-5813279 2.Centos 7 在虚拟机上安装好Centos7系统后,就可以开始准备安装D ...

  3. 05-spring框架—— Spring 事务

    5.1 Spring 的事务管理 事务原本是数据库中的概念,在 Dao 层.但一般情况下,需要将事务提升到业务层,即 Service 层.这样做是为了能够使用事务的特性来管理具体的业务. 在 Spri ...

  4. 4.3 jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20 分)中的一些问题

    1.Employee类的equals 由于题目要求//首先调用父类的equals方法,如果返回true.再比较company与salary.//比较salary属性时,使用DecimalFormat ...

  5. 第二章 Vue快速入门-- 27 字符串的padStart方法使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. sublime text 前端插件安装

    Package Control安装 打开sublime编辑器,ctrl + ` 打开安装PackageControl界面: sublime text3: import urllib.request,o ...

  7. golang之运算符

    目录 一.golang之运算符 1. 算术运算符 2. 关系运算符 3. 逻辑运算符 4. 位运算符 5. 赋值运算符 一.golang之运算符 Go 语言内置的运算符有:(比python少了一个成员 ...

  8. php的异步非阻塞swoole模块使用(一)实现简易tcp服务器--服务端

    绑定tcp服务器的地址 $swserver = new swoole_server("127.0.0.1",9501); 设置tcp服务器装机容量(太危言耸听了-其实就是设置属性) ...

  9. vue自定义指令,自动调用下载的方法

    directives: { clickDown: { inserted (el, binding, item) { if (+binding.value.item.fromId === +item.c ...

  10. Vue学习搭建(基础)

    空项目:https://github.com/ElementUI/element-starter.git 参考教程:https://blog.csdn.net/xuehu837769474/artic ...