标签:
主要是看N!结果中2的个数和5的个数,多的那个个数即是末尾0的个数
? ?
? ?
计算Z有两种方法
? ?
一种是对每个n都去看有多少个5的因子
? ?
一种是隔5个增加一个5的因子,隔25个再在之前的基础上增加一个5的因子
? ?
两种方法差不多
? ?
第二种,循环少
? ?
package numOfZeroFactorialN_2_2;
? ?
public class NumOfZeroFactorialN_2_2 {
? ?
static int numOfZeros(int n) {
int result = 0;
for (int i = 1; i <= n; i++) {
int j = i;
while (j % 5 == 0) {
result++;
j /= 5;
}
}
return result;
? ?
}
? ?
static int sol2(int n) {
int result = 0;
while (n != 0) {
result += n / 5;
n = n / 5;
}
? ?
return result;
? ?
}
? ?
public static void main(String[] args) {
System.out.println(sol2(10));
}
? ?
}
标签:
原文地址:http://www.cnblogs.com/keedor/p/4385482.html