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相关推荐

  1. LeetCode Factorial Trailing Zeroes(计算阶乘结果尾部有几个0)

    思路:计算n能被5,25,...b整除的个数 代码如下: public class Solution {public int trailingZeroes(int n){int sum = 0;whi ...

  2. LeetCode 172. Factorial Trailing Zeroes

    LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...

  3. 172. Factorial Trailing Zeroes

    /**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...

  4. leetcode python3 简单题172. Factorial Trailing Zeroes

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...

  5. 【LeetCode】172 - Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  6. Leetcode 172 Factorial Trailing Zeroes

    1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  7. C#LeetCode刷题之#172-阶乘后的零(Factorial Trailing Zeroes)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3854 访问. 给定一个整数 n,返回 n! 结果尾数中零的数量. ...

  8. LeetCode(172) Factorial Trailing Zeroes

    题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  9. Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

最新文章

  1. php 操作分表代码
  2. 折半查找一个有序数组中的一个元素,即二分法
  3. hdu 4496 并查集 逆向 并查集删边
  4. C语言变量未赋初值时,输出为乱七八糟解释
  5. linux删除默认路由
  6. “蚂蚁牙黑”太火,想玩就用ModelArts做一个!
  7. UVA10673 Play with Floor and Ceil【暴力枚举】
  8. VMware ESXi 8.0 SLIC 2.6 macOS Unlocker (Oct 2022 GA)
  9. 使用DBUtils报错connot create bean 错误解决的办法
  10. html方框打勾字段,HTML+CSS入门 如何设置 checkbox复选框控件的对勾√样式
  11. NAT基本原理与私有IP
  12. 源码看JAVA【五】Byte
  13. pip 如何指定国内源安装
  14. 折腾一下,Linux Mint 19 下工作
  15. cron表达式解析 + robfig/cron 源码剖析
  16. 关于eclipse SE版本不支持建立web工程的问题
  17. hadoop配置、运行错误总结
  18. HealthKit Swift 教程: workout
  19. [附源码]java毕业设计英语知识竞赛报名系统
  20. Java Object类型如何转换为其它类型呢?

热门文章

  1. 你的模型刚不刚?谷歌提出“刚度”概念,探索神经网络泛化新视角
  2. 距离产生美?k近邻算法python实现
  3. Tensorflow—继续优化,使MNIST准确率98%以上
  4. 理解人类世界的常识对于人工智能是一项挑战
  5. 10分钟了解图卷积神经网络的常用算法和发展方向
  6. AI破解脑电波,准确率超80%!高度还原你眼中最美的ta
  7. 重磅,2020年度第十届吴文俊人工智能科学技术奖获奖名单公示
  8. 美陆军将在2020年军演中测试人工智能新应用
  9. AR普及后的未来是怎样的?
  10. 牛津大学名誉教授Colin Blakemore:大脑是如何计算的?