leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
最初的代码
class Solution {
public:
int trailingZeroes(int n) {
long long int fac = 1;
int count=0;
if (n==0) fac = 1;
for(int i = n;i>0;i--)
{
fac *= i ;
}
while(fac % 10 == 0)
{
count ++;
fac = fac/10;
}
return count;
}
};
报错:
Submission Result: Time Limit Exceeded
看了网上的才知道是质数分解,主要是提取5的个数:
class Solution {
public:
int trailingZeroes(int n) {
int ret = ;
for(int i = ;i<=n;i++)
{
int tem = i;
while(tem % == )
{
ret ++;
tem /= ;
}
}
return ret;
}
};
但是 提交后突然 OMG!!!
Last executed input:1808548329
再次在网上寻找原因继续想,进一步简化:
首先1~n中既有5的倍数,还有25的倍数,还是125的倍数等等,而25可以提供两个0,125可以提供3个0,(PS:我自己没有证明过,求大神给出证明,欢迎联系QQ:543451622)
class Solution {
public:
int trailingZeroes(int n) {
int ret = ;
while(n)
{
ret += n/;
n /= ;
}
return ret;
}
};
Submission Result: Accepted
leetcode:Factorial Trailing Zeroes的更多相关文章
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 【leetcode】Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- ✡ 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】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 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 ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- LeetCode(31)-Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
随机推荐
- Codeforces Round #203 (Div. 2) A.TL
#include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; cin & ...
- Ubuntu根目录下各文件夹的功能详细介绍
Ubuntu的根目录下存在着很多的文件夹,但你知道他们都存放着哪些文件呢?这些是深入了解Ubuntu系统必不缺少的知识,本文就关于此做一下介绍吧. /bin/ 用以存储二进制可执行命令文件. / ...
- 使用RBTool自动提交code review请求
使用RBTool自动提交code review请求 前言 让我们回想一下手工提交review请求的过程: 首先得用 svn diff > filename.diff 生成diff文件. 然后输入 ...
- StereoBM::disp12MaxDiff Crash the Release
Initializing "cv::StereoBM bm.state->disp12MaxDiff" should be careful, inappropriate va ...
- 本函数用来改变目前 php 执行的目录到新的 directory 目录中
chdir : 改变目录. dir : 目录类别类. closedir : 关闭目录 handle. opendir : 打开目录 handle. readdir : 读取目录 handle. rew ...
- eclipse中project->clean的作用是什么
1.由于eclipse的编译是基于时间戳的判断机制的.因此当你按build all的时候有些eclipse认为时间戳没有改变的类不会被编译.因此你可以先clean一下再编译.这个时候eclipse ...
- 通过SocketLog快速分析OneThink程序
通过SocketLog快速分析OneThink程序 http://www.thinkphp.cn/topic/10846.html 浏览:2332 发布日期:2014/02/08 分类:技术分享 ...
- ICON文件保存
这两天想做一下windows系统下图标的修改,让程序有更新的时候能够更新图标的外观,达到提醒的作用,360,QQ经常采用这种方式进行更新的提示,也有采用弹框的方式来提示,用新版QVOD的同事可能见到过 ...
- Xamarin Visual Studio无法debug
在Visual Studio中,Target IOS Device下拉框是禁用状态,无法选择. Xamarin论坛中有不少关于这个问题的,如下面这个帖子: http://forums.xamarin. ...
- 详解在bash脚本中如何获取自身路径
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 这是stac ...