package liu.dh.algorithm1;
import java.math.BigInteger;
public class Test3_Jc {
/**
* @param args
* 使用递归:求n的阶乘尾部零的个数
*从Test2_Jc可以得到以下结果
* //分析:从结果可以看出,只要是5的倍数就增加一个零,5=1,10=2,15=3,20=4,25=6,30=7,35=8,40=9,45=10,
//25=5*5,5+5/5=6;
//45 =9*5,9+5/5=10
//可以看出:求n的阶乘尾部零的个数是有规律的:run(n)=n/5+run(n/5);
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getNum(1000));//249
}
public static int getNum(int num){
if (num <1) {
return 0;
}
return num/5+getNum(num/5);
}
}
/*//利用for循环求阶乘
BigInteger sum = new BigInteger("1");
for (int i = 1; i <=num; i++) {
//将i转化为Biginteger
BigInteger ii = new BigInteger(i+"");
sum = sum.multiply(ii);
}
//将sum转换字符串
String sumString = sum.toString();
return num;
}
}*/
|
|