|
C语言趣味程序百例精解之java实现(7)零的个数
程序:
public class Test7{
public static void main(String args[]){
numberOfZero();
}
/**
* 7.阶乘尾数0的个数 .分析:0的个数,2*5得一个零,转换成5的个数
*/
public static void numberOfZero() {
long start, over;
int count = 0;
start = System.currentTimeMillis();
for (int i = 1; i <=100; i++) {
if (i % 25 == 0) {
count += 2;
continue;
}
if (i % 5 == 0)
count++;
}
over = System.currentTimeMillis();
System.out.println("Method 1:100! contains " + count + " zero cost:"
+ (over - start));
start = System.currentTimeMillis();
// Method TWO
count = 0;
for (int i = 5; i <=100; i += 5) {
if (i % 25 == 0) {
count += 2;
} else
count++;
}
over = System.currentTimeMillis();
System.out.println("Method 2:100! contains " + count + " zero cost:"
+ (over - start));
}
}
C:\bat>java Test7
Method 1:100! contains 24 zero cost:0
Method 2:100! contains 24 zero cost:0 |
|