首页 > 试题广场 >

计算1000!得数后面的0的个数

[编程题]计算1000!得数后面的0的个数
  • 热度指数:743 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入任意一个正整数,计算出它的阶乘得数尾部有几个连续的0.  
题目编写完毕需要计算出1000的阶乘得数尾部有几个连续的0,需要把这个统计数字打印输出

例如:
3!= 6, 程序输出0 
5!=120, 程序输出1.

输入描述:
输入 1000


输出描述:
输出 最终得数尾部有多少个0
示例1

输入

3

输出

0
示例2

输入

5

输出

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

发表于 2021-03-05 10:21:04 回复(3)

/*
贴一个Java版本的
*/

import java.util.*;
public class Main
{
    /* Online Java Compiler and Editor */
     public static void main(String []args){

        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum=0;
        for(int k=5;k<=n;k=k+5){
            sum++;
            int sk=k/5;
            while(sk%5 ==0){
                sum++;
                sk=sk/5;
            }
        }
        System.out.print(sum);
        //String[] s=sc.nextLine().split(" ");

     }
}
发表于 2022-08-25 20:54:12 回复(0)
n=int(input())
if n<5 :
    ans=0
elif n==5 :
    ans=1
else:    
    i=5
    ans=0
    while i<n :
        ans=ans+(n//i)
        i=i*5 
print(ans)

发表于 2021-04-10 22:27:28 回复(0)
import sys
line=sys.stdin.readlines()
for lines in line:
    a=int(lines)
    ans=0
    while a>=5:
        a//=5
        ans+=a
    print(ans)
发表于 2021-04-08 17:09:49 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int mi = 5;
        int count = 0;
        while (mi <= i) {
            count += i / mi;
            mi *= 5;
        }
        System.out.println(count);
    }
}
发表于 2021-03-14 21:54:47 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int ans=0;
        for(int i=1;i<=n;i++){
            int tmp=i;
            while(tmp%5==0){//每个5和2组合成一个10
                ans++;
                tmp/=5;
            }
        }
        System.out.print(ans);
    }
}

发表于 2021-03-12 19:58:56 回复(0)
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
    String s = new Scanner(System.in).nextLine();
    //System.out.println(BigNumber(new BigInteger(s)).toString());
    String ch = BigNumber(new BigInteger(s)).toString();//取到阶层的字符
    int len = ch.length();//取字符的长度
    int count=0;//定义计数器
    for(int i=len-1;i>=0;i--){
        char a = ch.charAt(i);//取最后一位的字符
        if(a=='0') count++;
        else break;
    }
    System.out.println(count);
}
public static BigInteger BigNumber(BigInteger n){
    if(n.intValue()==1) return new BigInteger("1");
    
    BigInteger bt = new BigInteger("1");//设置bt的值为1 以便后面每次减1 然后递归
    BigInteger sb = n.subtract(bt);//使n每次减1赋值给sb
    return n.multiply(BigNumber(sb));    //相当于返回n*n-1
}
}
发表于 2021-03-05 21:13:25 回复(0)