172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码
文章目录
- 0 效果
- 1 题目
- 2 思路
- 3 代码
0 效果
1 题目
Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * … * 3 * 2 * 1.
Example 1:
Input: n = 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:Input: n = 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Example 3:Input: n = 0
Output: 0
Constraints:
0 <= n <= 104
2 思路
因为想要阶乘结果得到末尾带0的,就要乘以10,而10又是由2和5相乘。因此我们只用考虑阶乘因子中2和5的个数就可以。
而每个偶数都是2的倍数,因子都有含有2。在阶乘乘积中,2的个数远远大于5的个数,因此就只用考虑5的个数,计算出5的个数,就得到了0的个数(和5的个数相等)。
计算5的个数:通过找规律可以知道,阶乘中含有因子5的数等于循环计算这个数除以5的商(当商不为0时)的和。
找规律的方法:例如25!
中,含有5,10,15,20,25这5个含有因子5的数,但是25含有两个5,所以计算结果为25/5 + 25/5/5 = 6
。以此类推,可以得到125!
,含有5的数为125/5 + 125/5/5 + 125/5/5/5 = 31
。110!
含有5的数为110/5 + 110/5/5 = 26
。
3 代码
class Solution {public:int trailingZeroes(int n) {int cnt = 0;while(n != 0){cnt += (n/5);n /= 5;}return cnt;}
};
172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码相关推荐
- leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
数字的末尾为0实际上就是乘以了10,20.30.40其实本质上都是10,只不过是10的倍数.10只能通过2*5来获得,但是2的个数众多,用作判断不准确. 以20的阶乘为例子,造成末尾为0的数字其实就是 ...
- 172. Factorial Trailing Zeroes
/**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang* 首先别忘了什么是factorial,就是阶乘.那么很容易想到需要统计* (2,5)对的 ...
- LeetCode 172. Factorial Trailing Zeroes
LeetCode 172. Factorial Trailing Zeroes 问题来源LeetCode 172. Factorial Trailing Zeroes 问题描述 Given an in ...
- leetcode python3 简单题172. Factorial Trailing Zeroes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...
- Leetcode 172 Factorial Trailing Zeroes
1.题目要求 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 【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 ...
- LeetCode(172) Factorial Trailing Zeroes
题目如下: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 【leetcode❤python】172. Factorial Trailing Zeroes
#-*- coding: UTF-8 -*- #给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0. #所有的尾部的0可以看做都是2*5得来的, ...
最新文章
- 青少年编程竞赛交流群周报(第040周)
- 【CV】10种轻量级人脸检测算法大PK | 代码集合开源
- python兼职 在家工作-在家兼职的人或者是自由职业的人都在做什么?
- Deepin下配置JDK8
- python常用的装饰器有哪些_python基本装饰器
- 使用tensorflow object detection API 训练自己的目标检测模型 (三)
- docker教程_1 简介和安装
- jquery在IE8上使用find的问题
- Commons-FileUpload上传组件
- matlab ss2ss,Zemax+DDE+toolbox+for+Matlab 该工具箱可实现matlab编程和zemax通信 - 下载 - 搜珍网...
- SpringBoot配置SSL证书
- Iaas、Paas、Saas都是什么意思?
- RAM汇编指令DMB、DSB、ISB、SEV等
- 南理工计算机科学与工程学院,周俊龙 - 南京理工大学 - 计算机科学与工程学院...
- oracle rr与yy日期格式
- 三万字,100题!Linux知识汇总!
- mysql数据库 去除回车符,换行符,和空格
- 学习第一天——ipfs安装与使用
- 新闻稿写作结构有哪些类型?新闻稿写作要点是什么?
- 索尼黑卡相机Sony Rx100-M3与手机互联APP相关问题
热门文章
- 是时候该学会 MMDetection 进阶之非典型操作技能(一)
- CreateWindow() -- 创建普通的窗口
- ubuntu配置IP地址,网关,DNS和路由
- 边缘计算:一文理解云边端协同架构中的高性能云计算、边缘计算、云边协同
- 复试数据库系统概论(2)
- Word上的空白页无法删除,是因为在Word文档中有多种类型的格式标记,很多时候隐藏在页面中,无法看到这些标记,并占用文档区域,导致空白页无法直接删除。
- SLAM导航机器人零基础实战系列:(五)树莓派3开发环境搭建——2.安装ros-kinetic
- 考研复试——线性代数
- 【线性代数】线性相关与线性无关的定义与性质
- 看PDF时点击书签页面变小的解决方法