[LeetCode] Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解题思路:只有因子2和因子5相乘会产生10,同时因为因子2的数量大于因子5的数量,所以只需看序列中因子5的个数。它可以通过n/5得到,同时序列中还含有25,125,……这样的因子,其数量可以通过继续除5来得到。
实现代码:
/****************************************************************************** @COPYRIGHT NOTICE* @Copyright (c) 2015, 楚兴* @All rights reserved* @Version : 1.0* @Author : 楚兴* @Date : 2015/2/6 16:21* @Status : Accepted* @Runtime : 7 ms
*****************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:int trailingZeroes(int n) {int count = 0;while (n){count += n / 5;n /= 5;}return count;}
};int main()
{Solution s;for (int i = 1; i < 100; i++){cout<<s.trailingZeroes(i)<<endl;}system("pause");
}
[LeetCode] Factorial Trailing Zeroes相关推荐
- LeetCode Factorial Trailing Zeroes(计算阶乘结果尾部有几个0)
思路:计算n能被5,25,...b整除的个数 代码如下: public class Solution {public int trailingZeroes(int n){int sum = 0;whi ...
- LeetCode 172. Factorial Trailing Zeroes
LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...
- 172. Factorial Trailing Zeroes
/**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...
- leetcode python3 简单题172. Factorial Trailing Zeroes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...
- 【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 172 Factorial Trailing Zeroes
1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- C#LeetCode刷题之#172-阶乘后的零(Factorial Trailing Zeroes)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3854 访问. 给定一个整数 n,返回 n! 结果尾数中零的数量. ...
- LeetCode(172) Factorial Trailing Zeroes
题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
最新文章
- php 操作分表代码
- 折半查找一个有序数组中的一个元素,即二分法
- hdu 4496 并查集 逆向 并查集删边
- C语言变量未赋初值时,输出为乱七八糟解释
- linux删除默认路由
- “蚂蚁牙黑”太火,想玩就用ModelArts做一个!
- UVA10673 Play with Floor and Ceil【暴力枚举】
- VMware ESXi 8.0 SLIC 2.6 macOS Unlocker (Oct 2022 GA)
- 使用DBUtils报错connot create bean 错误解决的办法
- html方框打勾字段,HTML+CSS入门 如何设置 checkbox复选框控件的对勾√样式
- NAT基本原理与私有IP
- 源码看JAVA【五】Byte
- pip 如何指定国内源安装
- 折腾一下,Linux Mint 19 下工作
- cron表达式解析 + robfig/cron 源码剖析
- 关于eclipse SE版本不支持建立web工程的问题
- hadoop配置、运行错误总结
- HealthKit Swift 教程: workout
- [附源码]java毕业设计英语知识竞赛报名系统
- Java Object类型如何转换为其它类型呢?