//看着很简单的一道题,但是并不常规,考虑数据过大不能存储问题,必须用巧法来解决问题
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到1000之间任意数的阶乘后得到的数末尾0的个数
最新推荐文章于 2021-09-11 15:53:07 发布