码迷,mamicode.com
首页 > 其他好文 > 详细

N!结果中末尾0的个数 2.2

时间:2015-04-01 23:31:49      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

主要是看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));

}

? ?

}

N!结果中末尾0的个数 2.2

标签:

原文地址:http://www.cnblogs.com/keedor/p/4385482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!