2. Trailing Zeros【easy】
2. Trailing Zeros【easy】
Write an algorithm which computes the number of trailing zeros in n factorial.
11! = 39916800, so the out should be 2
O(log N) time
解法一:
class Solution {
/*
* param n: As desciption
* return: An integer, denote the number of trailing zeros in n!
*/
public long trailingZeros(long n) {
long count = ;
for(int i = ; Math.pow(,i) <= n; i++) {
count += n / (long)Math.pow(,i);//注意强制类型转换
}
return count;
}
};
求末尾0的个数:
至于一个数阶乘的末尾有多少个0,0的个数为(其中的“/”是取整除法):
例子:1000的阶乘末尾0的个数
1000/5 + 1000/25 + 1000/125 + 1000/625
= 200 + 40 + 8 + 1
= 249(个)
原理是:
假如你把1 × 2 ×3× 4 ×……×N中每一个因数分解质因数,结果就像:
1 × 2 × 3 × (2 × 2) × 5 × (2 × 3) × 7 × (2 × 2 ×2) ×……
10进制数结尾的每一个0都表示有一个因数10存在——任何进制都一样,对于一个M进制的数,让结尾多一个0就等价于乘以M。
10可以分解为2 × 5——因此只有质数2和5相乘能产生0,别的任何两个质数相乘都不能产生0,而且2,5相乘只产生一个0。
所以,分解后的整个因数式中有多少对(2, 5),结果中就有多少个0,而分解的结果中,2的个数显然是多于5的,因此,有多少个5,就有多少个(2, 5)对。
所以,讨论1000的阶乘结尾有几个0的问题,就被转换成了1到1000所有这些数的质因数分解式有多少个5的问题。
5的个数可以用上面那个式子算出,所以1000的阶乘结尾有249个0。
至于为什么用这个式子,可以见下例:
求26的阶乘的尾部有多少个0.
26! = 1 × 2 ×3× 4 × 5 × 6 × 7 × 8 × 9 × 10 × 11 × 12 × 13× 14 × 15 × 16 × 17 × 18 × 19 × 20 × 21 × 22 ×23× 24 × 25 × 26
内有 26/5 + 26/25 = 6(个)5相乘
因为25其实可以分解成2个5相乘,而26/5只计算了一个5,因此还要再加26/25。
参考自:http://blog.csdn.net/wutingyehe/article/details/46882181
解法二:
class Solution {
public:
// param n : description of n
// return: description of return
long long trailingZeros(long long n) {
long long sum = ;
while (n != ) {
sum += n / ;
n /= ;
}
return sum;
}
};
2. Trailing Zeros【easy】的更多相关文章
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
随机推荐
- Dom4jDemo应用-保存手机信息
---恢复内容开始--- import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStr ...
- 【Python笔记】Python语言基础
Python是一种解释性(没有编译).交互式.面向对象的语言 1.安装python编译器 版本:Python2.7比较普遍,Python不是向下兼容的软件,因此Python3.x有些东西不好找资料 2 ...
- 一道综合练习题实践list及dictionary集合类
定义一个员工的集合,对员工集合内的元素进行查询和删除.实现员工的签到和签退,要求如下: //A:每天只能签到一次 //B:签退前必须已经签到 //C:显示打卡记录 代码如下:员工信息类: using ...
- shell基本计算、逻辑运算、位运算详解
转:http://blog.chinaunix.net/uid-8504518-id-3918531.html Shell 提供大量的基本运算操作,在脚本中非常有用.Shell 对您提供的算术表达式求 ...
- easyui dialog 按钮动态命名
1.方法一: /** * grid新增 * 弹框并且获取支付类型 */ function gridAdd() { var dlg = $('#mydialog').dialog({ title : & ...
- javascript(JQuery)元素操作
html代码如下: <div id="picK"> <ul> <li style="float:left;width:90px;" ...
- jquery修改ajax的header的字段origin方法,均被浏览器拒绝
一.方法一 $.ajax({ headers: { Origin: "http://targetIP" } }); 二.方法二 $.ajax({ beforeSend: funct ...
- python安全编程
##入门 这将是第一个一系列关于python编程的博客文章.python是一门非常强大的语言,因为它有信息安全社区的支撑.这意味着很多工具都是由python编写并且可以在脚本中调用很多模块.使用模块的 ...
- 【Hadoop】HDFS客户端开发示例
1.原理.步骤 2.HDFS客户端示例代码 package com.ares.hadoop.hdfs; import java.io.FileInputStream; import java.io.F ...
- [Angular] Angular Custom Change Detection with ChangeDetectorRef
Each component has its own ChangeDetectorRef, and we can inject ChangeDetectorRef into constructor: ...