/**
* n! 多少个0
* 实际就是5的个数
*/
public class Zero {
public static int count(int n){
if(n < 1){
return -1;
}
int count = 0;
for(int i=1; i<=n; i++){
int m = i;
while(m%5 == 0){
count++;
m = m/5;
}
}
return count;
}
public static int count2(int n){
if(n < 1){
return -1;
}
int count = 0;
while(n>0){
int m = n;
while(m%5 == 0){
count++;
m = m/5;
}
n--;
}
return count;
}
public static int count3(int n){
if(n < 1){
return -1;
}
int count = 0;
while(n > 0){
count += n/5;
n = n/5;
}
return count;
}
public static long by(int n){
long m = 1;
while(n>0){
n--;
m *= (n+1)*n;
n--;
}
return m;
}
/**
* @param args
*/
public static void main(String[] args) {
int count = Zero.count(10);
int count2 = Zero.count2(10);
int count3 = Zero.count2(10);
long number = Zero.by(10);
System.out.println(number);
System.out.println(count);
System.out.println(count2);
System.out.println(count3);
}
}n! 多少个0
最新推荐文章于 2019-03-30 15:24:59 发布
本文介绍了一种计算阶乘中尾随零数量的方法。通过三种不同的算法实现,分别是逐个数字检查、逐减计数以及高效递归除法。这些方法能够帮助理解如何有效地找出阶乘结果中末尾零的数量。
714

被折叠的 条评论
为什么被折叠?



