读书人

1024! 末后有多少个0

发布时间: 2012-09-22 21:54:54 作者: rapoo

1024! 末尾有多少个0?

public class CountZerosInFactorial {/** * 题目:1024! 末尾有多少个0? * 参看《编程之美》 * 解答: 末尾0的个数取决于乘法中因子2和5的个数。显然乘法中因子2的个数大于5的个数,所以我们只需统计因子5的个数。是5的倍数的数有: 1024 / 5 = 204个是25的倍数的数有:1024 / 25 = 40个是125的倍数的数有:1024 / 125 = 8个是625的倍数的数有:1024 / 625 = 1个所以1024! 中总共有204+40+8+1=253个因子5。也就是说1024! 末尾有253个0 */public static void main(String[] args) {int tmp=countZerosInFactorial(1024);System.out.println(tmp);}public static int countZerosInFactorial(int n){if(n<5){return -1;}int count=0;while(n>0){count+=n/5;n/=5;}return count;}}

读书人网 >编程

热点推荐