LanQiaoTestCodes/求100阶乘0的个数.java

42 lines
798 B
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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