哪几个数的阶乘末尾有n个0?其中n是一个正整数,从键盘输入

题目:哪几个数的阶乘末尾有n个0?其中n是一个正整数,从键盘输入。

 1 int main( void )    /* name: zerotail.cpp */
 2 {   int num, n, c, m;
 3     cout<<"输入零的个数(>0):";    cin>>n;
 4     while( n>0 )
 5     {   c=0;    num=0;
 6         do
 7         {    num+=5;
 8             m=num;
 9             while( m%5==0 )
10             {    c++;
11                 m/=5;
12             }
13         }while( c<n );
14         if( c==n )
15             cout<<num<<‘,‘<<num+1<<‘,‘<<num+2<<‘,‘<<num+3<<‘,‘<<num+4
16                 <<"的阶乘末尾有"<<n<<"个零。"<<endl;
17         else
18             cout<<"不存在这样的数!"<<endl;
19
20         cout<<"\n输入零的个数(>0):";    cin>>n;
21     }
22     return 0;
23 }
时间: 2024-12-09 19:35:33

哪几个数的阶乘末尾有n个0?其中n是一个正整数,从键盘输入的相关文章

172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (1) class Solution { public: int trailingZeroes(int n) { int ans = 0; for(long long i = 5; n / i; i *= 5) { ans += n / i; }

在n个任意不相同的数中,输出r个数的组合,并且n和r由键盘输入。

主要是运用递归的思想,函数主要两个参数,point是上一次取到的位置,picked主要记录在数组b中所取的元素的个数,函数的每一轮递归都会取一个数.   1 package pack; 2 import java.util.*; 3 4 public class demo_1 { 5 static int r,n; 6 static int a[]=new int [100]; 7 static int b[]=new int[100]; 8 static void f(int point,in

求一个数阶乘末尾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 /

NYOJ1026 阶乘末尾非0 【模板】

阶乘末尾非0 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 我们的问题很是简单,n!末尾非0数是几? 比如n=5的时候,n!=120,那么n!末尾非0数是2. 输入 多组数据, 每组数据占一行,每行一个整数0<=n<=10^1000 输出 n!末尾非0数. 样例输入 5 样例输出 2 直接用的网上的模板 /*==================================================*| 阶乘最后非零位,复杂度 O(nlogn) \*==

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 /=

计算阶乘n!末尾所含的0的个数

问题描述 给定参数n(n为正整数),请计算n的阶乘n!末尾所含有"0"的个数. 例如,5!=120,其末尾所含有的"0"的个数为1:10!= 3628800,其末尾所含有的"0"的个数为2:20!= 2432902008176640000,其末尾所含有的"0"的个数为4. 问题分析: 显然,对于阶乘增长速度的非常快的,很容易就溢出了.当然就不可能把阶乘算出来,而是要找规律解决.下面用因式分解的思路来考虑:末尾的0可以分解为2*

[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的个数,

第16周《C++语言基础》实践参考——有些数的阶乘不算了

问题描述: 项目2-有些数的阶乘不算了 求n!的函数,当用户的输入为负数,以及输入数太大时(例如大于12),使用异常处理机制予以拒绝,并给出恰当的提示. 代码实现: #include <iostream> #include <cstdio> using namespace std; int main(){ int n; try{ printf("请输入一个数:"); scanf("%d",&n); if(n<0)throw 1;

阶乘末尾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; //保存上一个阶乘 的