阶乘末尾0的个数

问题很简单:给定一个整数n,那么n的阶乘末尾有多少个连续的0呢?

那个可以去求这个阶乘吗,大数的代码上去一套?这个大数求出来肯定是很慢的,还有其他方法吗,你可以观察一个产生0,本质是2*5,出现了2一定会出现5,2的个数会大于5的个数,所以我可以直接统计5的个数,统计每个数有几个因子5

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while (cin >> n)
    {
        int num = 0 ,i;
        for(int m=1;m<=n;m++)
        {
            i=m;
            while (i % 5 == 0)
            {
                i=i/5;
                num++;
            }
        }
        cout << num << endl;
    }
}

当然可以直接关注5

f(5!) = 1 + f(1!) = 1
f(10!) = 2 + f(2!) = 2
f(20!) = 4 + f(4!) = 4
f(100!) = 20 + f(20!) = 20 + 4 + f(4!) = 24
f(1000!) = 200 + f(200!) = 200 + 40 + f(40!) = 240 + 8 + f(8!) = 248 + 1 + f(1) =249

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while (cin >> n)
    {
        int num = 0;
        while (n > 0) {
            num += n / 5;
            n /= 5;
        }
        cout << num << endl;
    }
}

原文地址:https://www.cnblogs.com/BobHuang/p/12245428.html

时间: 2024-08-01 00:27:02

阶乘末尾0的个数的相关文章

阶乘末尾0的个数(证明)

先给出算法: 给定n,求n的阶乘末尾0的个数. int res = 0; while (n > 0) { res += n / 5; n /= 5; } 因为: 比方说求15的阶乘,也就是求 1 × 2 × 3 × 4 × 5 × 6 × 7 × 8 × 9 × 10 × 11 × 12 × 13 × 14 × 15 的末尾0的个数.现在我们把这15个数分解出来含有5的因子 1 × 2 × 3 × 4 × 5 × 6 × 7 × 8 × 9 × 2   × 11 × 12 × 13 × 14 ×

N的阶乘末尾0的个数和其二进制表示中最后位1的位置

问题一解法: 我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个10呢? 其实我们只要算里面有多少个5就可以了? 因为在这些分解后的因子中,能产生10的可只有5和2相乘了,由于2的个数是大于5的个数的,因此 我们只要算5的个数就可以了.那么这个题目就是算这些从1到N的数字分解成因子后,这些因子里面 5的个数. Python代码 def factorialnumberof

求一个数阶乘末尾0的个数

#include<iostream> using namespace std; //给定一个整数N,那么N的阶乘末尾有几个0?N=10,N!=3628800,末尾有2个0 //1.如果我们从"哪些数相乘能得到 10"这个角度来考虑,问题就变得简单了. //首先考虑,如果 N!= K×10M,且 K 不能被 10 整除,那么 N!末尾有 M 个 0.再考虑 //对 N!进行质因数分解,N!=(2x)×(3y)×(5z)-,由于 10 = 2×5,所以 M 只跟 X 和 Z /

Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 Note中提示让用对数的时间复杂度求解,那么如果粗暴的算出N的阶乘然后看末尾0的个数是不可能的. 所以仔细分析,N! = 1 * 2 * 3 * ... * N 而末尾0的个数只与这些乘数中5和2的个数有关,因为每出现一对5和2就会产生

阶乘末尾0的个数问题

问题:给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0,写出算法. 回答: int countZero(int N) {    int ans = 0;    int maxInt = 1000000000;//10^9    int  tmpn = N;    while(tmpn){        maxInt /= 10;        tmpn /= 10;    }    int lastBit = 1; //保存上一个阶乘 的

LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 &amp; 二分

题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出impossible 可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可; LL Find(LL x) { LL ans = 0; while(x) { ans += x/5; x /=

p74 阶乘末尾0的个数(leetcode 172)

一:解题思路 Time:O(log_5(n)),Space:O(1) 二:完整代码示例 (C++版和Java版) C++: class Solution { public: int trailingZeroes(int n) { int count = 0; while (n > 0) { n /= 5; count += n; } return count; } }; Java: class Solution { public int trailingZeroes(int n) { int c

[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. Credits:Special thanks to @ts for adding this problem and creating all test cases. 这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中10的个数,

算法-计算阶乘n!末尾0的个数

算法逻辑转载自计算阶乘n!末尾0的个数: 问题描述    给定参数n(n为正整数),请计算n的阶乘n!末尾所含有"0"的个数.    例如,5!=120,其末尾所含有的"0"的个数为1:10!= 3628800,其末尾所含有的"0"的个数为2:20!= 2432902008176640000,其末尾所含有的"0"的个数为4. 计算公式    这里先给出其计算公式,后面给出推导过程.    令f(x)表示正整数x末尾所含有的&q