标签:面试题
how many does the n factorial end have zero?
个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:google搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客内容:how many does the factorial of n have zero?
博客时间:2014-5-5;
编程语言:Java ;
编程坏境:Windows 7 专业版 x64;
编程工具:jdk,eclipse;
制图工具:office 2010 ppt;
硬件信息:7G-3 笔记本;
my words
nature is so beautiful.
problem( from beauty of programming )
how many do the factorial of n have zero?
my solution
first solution: get the n factorial, then get how many zero the end of it has.
time cost: O(n)
second solution: 1 * 2 * 3 * 4 * ···· * n. we can know zero form 2 * 5, and know that the number of 2 is more than that of 5 in these factors. So we get the number of 5.
1) we can computer how many 5 factor every number from 1 to n have.
2) we can use n to get all the sum of number of 5 factor.
int _Number_n_factorial(int n) { int i = n; int result = 0; while(i > 0) { result += i/5 ; i = i/5; } return result; }
program run out:
the line of 1 is 0 the line of 2 is 0 the line of 3 is 0 the line of 4 is 0 the line of 5 is 1 the line of 6 is 1 the line of 7 is 1 the line of 8 is 1 the line of 9 is 1 the line of 10 is 2 the line of 11 is 2 the line of 12 is 2 the line of 13 is 2 the line of 14 is 2 the line of 15 is 3 the line of 16 is 3 the line of 17 is 3 the line of 18 is 3 the line of 19 is 3 the line of 20 is 4 the line of 21 is 4 the line of 22 is 4 the line of 23 is 4 the line of 24 is 4 the line of 25 is 6 the line of 26 is 6 the line of 27 is 6 the line of 28 is 6 the line of 29 is 6 the line of 30 is 7 the line of 31 is 7 the line of 32 is 7 the line of 33 is 7 the line of 34 is 7 the line of 35 is 8 the line of 36 is 8 the line of 37 is 8 the line of 38 is 8 the line of 39 is 8 the line of 40 is 9 the line of 41 is 9 the line of 42 is 9 the line of 43 is 9 the line of 44 is 9 the line of 45 is 10 the line of 46 is 10 the line of 47 is 10 the line of 48 is 10 the line of 49 is 10 the line of 50 is 12 the line of 51 is 12 the line of 52 is 12 the line of 53 is 12 the line of 54 is 12 the line of 55 is 13 the line of 56 is 13 the line of 57 is 13 the line of 58 is 13 the line of 59 is 13 the line of 60 is 14 the line of 61 is 14 the line of 62 is 14 the line of 63 is 14 the line of 64 is 14 the line of 65 is 15 the line of 66 is 15 the line of 67 is 15 the line of 68 is 15 the line of 69 is 15 the line of 70 is 16 the line of 71 is 16 the line of 72 is 16 the line of 73 is 16 the line of 74 is 16 the line of 75 is 18 the line of 76 is 18 the line of 77 is 18 the line of 78 is 18 the line of 79 is 18 the line of 80 is 19 the line of 81 is 19 the line of 82 is 19 the line of 83 is 19 the line of 84 is 19 the line of 85 is 20 the line of 86 is 20 the line of 87 is 20 the line of 88 is 20 the line of 89 is 20 the line of 90 is 21 the line of 91 is 21 the line of 92 is 21 the line of 93 is 21 the line of 94 is 21 the line of 95 is 22 the line of 96 is 22 the line of 97 is 22 the line of 98 is 22 the line of 99 is 22
my code
test.java
package test; public class test { public static void main(String[] args) { //System.in.read(); int i = 1; int result; while( i<100 ) { result = _Number_n_factorial(i); System.out.println("the line of "+i+" is "+result); i = i+1; } } public static int _Number_n_factorial(int n) { int i = n; int result = 0; while(i > 0) { result += i/5 ; i = i/5; } return result; } }
how many does the factorial of n have zero?,布布扣,bubuko.com
how many does the factorial of n have zero?
标签:面试题
原文地址:http://blog.csdn.net/cqs_experiment/article/details/25104723