LintCode(2)-尾部的零

有多少个零主要看有多少个5,2肯定足够

long long trailingZeros(long long n) {

        // write your code here

        long long x = 5;//注意数据类型,若为int,输入过大时,会远小于这个值

        long long sum = 0;

        for(int i = 1; i < 36; i++)

        {

            if(n/x)

            {

                sum = sum + n / x;

                x = x * 5;

            }

            else

            {

                break;

            }

        }

        return sum;

    }

猜你喜欢

转载自blog.csdn.net/Qmingwt/article/details/125821227