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 ...
随机推荐
- iOS去掉字符串中的HTML标签的方法
方法一.NSScanner去除标签 - (NSString *)removeTheHtmlFromString:(NSString *)htmlString { NSScanner * scanner ...
- Mac下安装svn服务器
本文转载自http://www.cnblogs.com/czq1989/p/4913692.html Mac默认已经安装了svn,我们只需要进行配置并开启就可以了 首先我们可以验证一下是否安装了svn ...
- SVN 安装教程
安装包:http://pan.baidu.com/s/1kTTcbJp 安装步骤看这个博主的就好了: http://www.cnblogs.com/xing901022/p/4399382.html ...
- 【bzoj2523】【CTSC2001】聪明的学生
真是神仙题,做完后感觉智商提(jiang)升(di)了 这种题一般都是把局面设成状态,然后发现可以由一种状态转移到另一种状态,那就是 $dp$ 了. 但是这道题怎么设呢? 题目中给了你一个结论,一般题 ...
- datax二次开发
从hive抽取数据,写入hbase 一.datax插件hbase12xwriter开发 查看datax源码,发现有hbase094xwriter和hbase11xwriter源码,说明datax支持h ...
- Ubuntu下查看so文件的函数列表
Ubuntu下查看so文件的函数列表 可使用如下命令: 1.nm -D XXX.so 2.objdump -tT XXX.so nm libcyusb.so | grep "usb ...
- 〇三——css常规使用
我们在前面已经学习了常用的html基础,就可以画出一个最直接的‘裸体’ ,那么这么画出来的比较简陋,那怎么能让他变得更漂亮呢?这里就引出今天要讲的——css 我们先看看怎么把页面加上修饰的效果 < ...
- Java I/O(一)流和文件类File的概述、FileInputStream和FileInputStream
一.流概述 & InputStream.OutputStream 流包括输入流和输出流,即I/O(Input和Output),具体结构如下: I/O类都被放在java.io包中,所有的输入流类 ...
- python基础(变量、基础数据类型、流程控制)
今日内容html {overflow-x: initial !important;}:root { --bg-color:#ffffff; --text-color:#333333; --select ...
- Python语法汇总
如果你之前学过任何一门编程语言,因为每种语言的基础语法要做的事情其实基本是相同的,只是表示方式或某些地方稍稍不同,因此在学Python的时候将它与其它你已经掌握的编程语言对比着学,这样学起来更快,效果 ...