2. Trailing Zeros

灰太狼 2022-05-26 20:44 323阅读 0赞

2. Trailing Zeros

Description

  1. Write an algorithm which computes the number of trailing zeros in n factorial.

Example

  1. 11! = 39916800, so the out should be 2

Solution

  1. public class Solution {
  2. /* * @param n: An integer * @return: An integer, denote the number of trailing zeros in n! */
  3. public static long trailingZeros(long n) {
  4. // write your code here, try to do it without arithmetic operators.
  5. long s = 0;
  6. for(long i=5;n/i>=1;i*=5){
  7. s += n/i;
  8. }
  9. return s;
  10. }
  11. }

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读