三大国粹,月梅汁,手机充值卡在哪批发
write an algorithm which computes the number of trailing zeros in n factorial.
11! = 39916800, so the out should be 2
o(log n) time
1 /* 2 * @param n: a long integer 3 * @return: an integer, denote the number of trailing zeros in n! 4 */ 5 long long trailingzeros(long long n) { 6 // write your code here, try to do it without arithmetic operators. 7 if(n<5){ 8 return 0; 9 } 10 else{ 11 return n/5 + trailingzeros(n/5); 12 } 13 }
this solution is implemented by a recursive method, we can also use a loop method to solve this problem.
1 /* 2 * @param n: a long integer 3 * @return: an integer, denote the number of trailing zeros in n! 4 */ 5 long long trailingzeros(long long n) { 6 // write your code here, try to do it without arithmetic operators. 7 long long result = 0; 8 while ( n > 0) 9 { 10 result += n/5; 11 n /= 5; 12 } 13 14 return result; 15 }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论