设计一个算法,计算出n阶乘中尾部零的个数

    技术2024-10-21  22

    采用递归的算法来实现阶乘的结尾零的数量

    #include <iostream> using namespace std; class Solution { public: /* * @param n: A long integer * @return: An integer, denote the number of trailing zeros in n! */ long long trailingZeros(long long n) { int num = multi(n); int count = 0; while ((num % 10) == 0) { count++; num = num / 10; } return count; } private: int multi(long long n) { if (n == 1) { return n; } return n * multi(n - 1); } };
    Processed: 0.009, SQL: 9