Math---Trailing Zeros

来源:互联网 发布:桌面 台历 纸质 知乎 编辑:程序博客网 时间:2024/05/16 11:20

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

11! = 39916800, so the out should be 2

class Solution {    /*     * param n: As desciption     * return: An integer, denote the number of trailing zeros in n!     */    public long trailingZeros(long n) {        // write your code here        long sum = 0;  //防止溢出        while(n!=0){          sum += n/5;          n = n/5;        }        return sum;    }};
0 0
原创粉丝点击