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

//看着很简单的一道题,但是并不常规,考虑数据过大不能存储问题,必须用巧法来解决问题

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;
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值