42 lines
798 B
Java
42 lines
798 B
Java
package Course_Codes;
|
||
|
||
import java.math.BigInteger;
|
||
|
||
public class 求100阶乘0的个数 {
|
||
|
||
public static void main(String[] args) {
|
||
int n=10;
|
||
Fun(n);
|
||
Fun2(n);
|
||
}
|
||
|
||
public static void Fun(int n) {
|
||
BigInteger a=new BigInteger("1");
|
||
for(int i=1;i<=n;i++) {
|
||
//a为BigInteger 类型,因此不可用a*=i;
|
||
a=a.multiply(BigInteger.valueOf(i));
|
||
|
||
}
|
||
String s=a.toString();//s=100!的值
|
||
System.out.println(s);
|
||
int count=0;
|
||
for(int i=s.length()-1;i>=0;i--) {
|
||
if(s.charAt(i)!='0')//找到第一个非0的数
|
||
break;
|
||
count++;//记录0的个数
|
||
}
|
||
System.out.println(count);
|
||
}
|
||
//数学方法
|
||
public static void Fun2(int n) {
|
||
int ans=0;
|
||
while(n>0) {
|
||
//因数中有1个5就有1个0,即计算5的个数
|
||
ans=ans+n/5;
|
||
n=n/5;
|
||
}
|
||
System.out.println(ans);
|
||
}
|
||
|
||
}
|