阶乘末尾的零个数

[编程题] 末尾0的个数

输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2

输入描述:
输入为一行,n(1 ≤ n ≤ 1000)
输出描述:
输出一个整数,即题目所求
输入例子:
10
输出例子:
2方法:Z = N/5 + N /(5*5) + N/(5*5*5).....知道N/(5的K次方)
 1 import java.util.Scanner;
 2
 3 public class Test60 {
 4
 5     public static void main(String[] args) {
 6
 7         Scanner in = new Scanner(System.in);
 8         int n = in.nextInt();
 9         int count = 0;
10         while(n>0){
11             count += n/5;
12             n = n/5;
13         }
14         System.out.println(count);
15         in.close();
16     }
17
18 }
时间: 2025-01-01 13:39:04

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

阶乘末尾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的阶乘末尾有多少个连续的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

阶乘末尾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 /=

[LeetCode]65. Factorial Trailing Zeros阶乘的尾零个数

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. Subscribe to see which companies asked th

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