找0到1000之间任意数的阶乘后得到的数末尾0的个数

来源:互联网 发布:五婶软件下载 编辑:程序博客网 时间:2024/06/14 01:11
//看着很简单的一道题,但是并不常规,考虑数据过大不能存储问题,必须用巧法来解决问题package Day40;/*输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2输入为一行,n(1 ≤ n ≤ 1000)*/import java.util.*;public class Test1 {public static void main(String[] args) {    Scanner sc =new Scanner(System.in);      int n =sc.nextInt();       System.out.println(fun(n));    //   System.out.println(n/=5);}//对输入值进行处理public static int fun(int n){int count=0;while(n!=0){count +=n/5;n /=5;}return count;}}//牛人的思路参考,但不通用,1000以上就不再符合,需要对结果取更大数例如1000的余数package Day40;/*输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2输入为一行,n(1 ≤ n ≤ 1000)*///出现的问题是阶乘得到的数太大,存不下,解决办法将得到的数处理。import java.util.*;public class Test {public static void main(String[] args) { Scanner sc =new Scanner(System.in);      int n =sc.nextInt();       System.out.println(fun(n));}public static int fun(int n){int count=0;long f=1;for(int i=1;i<=n;i++){   f=f*i;while(f%10==0){count=count+1;f=f/10;}f=f%100;}return count;}}