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 = 0;while(n){count += n/5;n = n/5;}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。
转载于:https://www.cnblogs.com/ymjyqsx/p/9652147.html
leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)相关推荐
- LeetCode 172. Factorial Trailing Zeroes
LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...
- Leetcode 172 Factorial Trailing Zeroes
1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码
文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...
- LeetCode(172) Factorial Trailing Zeroes
题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 172. Factorial Trailing Zeroes
/**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...
- 【leetcode❤python】172. Factorial Trailing Zeroes
#-*- coding: UTF-8 -*- #给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0. #所有的尾部的0可以看做都是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 ...
- Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
最新文章
- 听说你想从事中间件开发?
- linux epoll 文件,Linux面试必知:一句话讲透epoll-文件句柄
- 8. An Introduction to MCMC for Machine Learning (3)
- 牛客 - Across the Firewall(最大流)
- POJ1177矩形面积并(矩形切割+括号匹配)
- CSDN开放90%页面缩放阅读博文的惊喜
- 29.课时29.【Django模板】url标签使用详解(Av61533158,P29)
- java indexof 通配符,字符串与含有通配符‘*’的字符串匹配(非正则表达式)
- 化工计算机软件基础考试题,2013年化工工程师基础知识网友版真题(下午卷)...
- Python图像纹理分割
- 基于.net平台的Windows窗体应用程序——(已附源码)学生管理系统
- 从零开始学Android应用安全测试
- 【C++】字体文件解析(FreeType)
- SIFT与SURF算法
- 计算机科学与技术专业宣传口号,十大经典深入人心科技类广告语
- 1138: C语言合法标识符
- 【简单利用函数实现多条件求和】
- 为什么ppt图形卡配置不正确_PPT常见问题解决方法,PPT检测到图形卡可能配置不正确怎么办?...
- Tomcat多实例与负载均衡
- c++椭圆最小二乘法原理_利用最小二乘法拟合椭圆方程的理论推导,附有matlab代码...
热门文章
- 用easyx画五角星_冷军画了一把破剪刀, 竟然卖到了160万,被专家抨击:乱涂鸦!得奖后专家顿时不说话了...
- 美赛整理之遗传算法优化BP神经网络的齿轮故障诊断问题
- github 怎么搜索_实用!8个玩转GitHub的小插件,快来试试吧!
- c++ 三角函数_精准备考 | 初中数学三角函数知识点全归纳
- java闭包lambda,(lambda)函数闭包捕获了什么?
- monggodb和MySQL同步问题_monggoDB添加到windows服务
- Hive压缩存储(以Parquet为例)
- DataNode 运行状况
- UML类图关系全面剖析[转]
- 绕开“陷阱“,阿里专家带你深入理解C++对象模型的特殊之处