Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
给定一个整数n,返回n!(n的阶乘)数字中的后缀0的个数。
考虑n!的质数因子。后缀0总是由质因子2和质因子5相乘得来的。如果我们可以计数2和5的个数,问题就解决了。考虑下面的例子:
n = 5: 5!的质因子中 (2 * 2 * 2 * 3 * 5)包含一个5和三个2。因而后缀0的个数是1。
n = 11: 11!的质因子中(2^8 * 3^4 * 5^2 * 7)包含两个5和三个2。于是后缀0的个数就是2。
我们很容易观察到质因子中2的个数总是大于等于5的个数。因此只要计数5的个数就可以了。那么怎样计算n!的质因子中所有5的个数呢?一个简单的方法是计算floor(n/5)。例如,7!有一个5,10!有两个5。除此之外,还有一件事情要考虑。诸如25,125之类的数字有不止一个5。例如,如果我们考虑28!,我们得到一个额外的5,并且0的总数变成了6。处理这个问题也很简单,首先对n÷5,移除所有的单个5,然后÷25,移除额外的5,以此类推。下面是归纳出的计算后缀0的公式。
n!后缀0的个数 = n!质因子中5的个数= floor(n/5) + floor(n/25) + floor(n/125) + ....
class Solution {
public:int trailingZeroes(int n) {int result = 0;while(n){result += n/5;n /= 5;}return result; }
};
#include<iostream>
#include<algorithm>
using namespace std;
int getnum(int);
int main()
{int a[] = { 12, 35, 7, 79, 36 };for (auto data : a){cout << data << ' ' << getnum(data) << endl;}system("pause");return 0;
}int getnum(int n)
{int num = 0;/*while (n){num = num + n / 5;n = n / 5;}return num;*/int j;for (int i = 1; i <= n; i++){j = i;while ((j%5)== 0){j /= 5;num++;}}return num;
}
Factorial Trailing Zeroes相关推荐
- 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] 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
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
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 python3 简单题172. Factorial Trailing Zeroes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十二题 (1)题目 英文: Given an integer n, retu ...
- 172 Factorial Trailing Zeroes(阶乘后的零)————附带详细思路和代码
文章目录 0 效果 1 题目 2 思路 3 代码 0 效果 1 题目 Given an integer n, return the number of trailing zeroes in n!. N ...
最新文章
- 阿里2018营收2502亿元,云计算业务增幅超100%
- 1哈哈欢迎使用CSDN-markdown编辑器
- 企业生产常用的磁盘管理方式介绍
- Python魔法函数
- Sublime Text Version 3.0,Build3143注册码
- 网关层面为何要用Lua
- 在python中,用正则表达式提取多层括号中最外层括号包含的内容
- 适用于Java开发人员的Groovy吗? 认识Gradle,Grails和Spock
- 【复习】使用 SQLiteDatabase 操作 SQLite 数据库
- Java I/O 扩展
- m7405d粉盒清零方法_联想打印机计数器怎么清零?一体机硒鼓怎么清零?看这一篇就够...
- DWR中引用JS的路径问题
- 基于JAVA+SpringBoot+Mybatis+MYSQL的酒店管理系统
- C中的C文件与h文件辨析(转)
- 【3dmax千千问】初学3dmax插件神器第18课|VRAY渲染教程|疯狂模渲大师用3dmax插件神器的扫描线渲染器该怎么表现效果图的写实效果?
- 实现简单的英文字母大小写转换
- php curl 417,PHP Curl 417 Expectation Failed解决办法
- Ubuntu下VSCode调试C++程序以及opencv库和Qt5库
- 怎么获取公众号二维码?
- 弹出框样式,swal is not define