leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为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)的更多相关文章
- ✡ 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 ...
- [LeetCode]172. Factorial Trailing Zeroes阶乘尾随0的个数
所有的0都是有2和45相乘得'到的,而在1-n中,2的个数是比5多的,所以找5的个数就行 但是不要忘了25中包含两个5,125中包含3个5,以此类推 所以在找完1-n中先找5,再找25,再找125.. ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- 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 ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 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 ...
- 172 Factorial Trailing Zeroes 阶乘后的零
给定一个整数 n,返回 n! 结果尾数中零的数量.注意: 你的解决方案应为对数时间复杂度. 详见:https://leetcode.com/problems/factorial-trailing-ze ...
- Leetcode 172 Factorial Trailing Zeroes
给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...
随机推荐
- UITableViewCell背景色.选中背景色,分割线,字体颜色设置
1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...
- spring 在web.xml 里面如何使用多个xml配置文件
1, 在web.xml中定义 contextConfigLocation参数.spring会使用这个参数加载.所有逗号分割的xml.如果没有这个参数,spring默认加载web-inf/applica ...
- LInux安装MySQL5.7.24详情
安装包下载 MySQL 的官网下载地址:http://www.mysql.com/downloads/ 我安装的是5.7版本 第二步: 选择:TAR (mysql-5.7.24-el7-x86_64. ...
- php检测函数是否存在函数 function_exists
php检测函数是否存在函数 function_exists 语法 bool function_exists ( string $function_name )检查的定义的函数的列表,同时内置(内部)和 ...
- Codeforces 991 车牌号组合数学计算
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]
[易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...
- JS转为number的四种方法
// 1.Number() var num1 = Number(true); console.log(num1); var num2 = Number(" ") console.l ...
- linux 下mysql忘记密码或者安装好linux后不知道mysql初始密码解决方案
1.使用yum安装mysql后 2.初始密码在/var/log/mysqld.log这个文件里 3.输入命令:grep 'temporary password' /var/log/mysqld.log ...
- 树形结构根据最后一位的id匹配整个路径
function recursionTreeId(_arr, _id) { _arr.forEach(item => { if (item.id === _id) { optionArr.uns ...
- 关于深度学习之TensorFlow简单实例
1.对TensorFlow的基本操作 import tensorflow as tf import os os.environ[" a=tf.constant(2) b=tf.constan ...