solution.md 1.4 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
# 阶乘后的零

<p>给定一个整数 <code>n</code> ,返回 <code>n!</code> 结果中尾随零的数量。</p>

<p>提示&nbsp;<code>n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1</code></p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>n = 3
<strong>输出:</strong>0
<strong>解释:</strong>3! = 6 ,不含尾随 0
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>n = 5
<strong>输出:</strong>1
<strong>解释:</strong>5! = 120 ,有一个尾随 0
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>n = 0
<strong>输出:</strong>0
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>0 &lt;= n &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>

<p><b>进阶:</b>你可以设计并实现对数时间复杂度的算法来解决此问题吗?</p>


## template

```cpp
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int trailingZeroes(int n)
    {
        int numOfZeros = 0;

        while (n > 0)
        {
            numOfZeros += numOf5(n);
            n--;
        }

        return numOfZeros;
    }
    int numOf5(int num)
    {
        int count = 0;
        while ((num > 1) && (num % 5 == 0))
        {
            count++;
            num /= 5;
        }
        return count;
    }
};


```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```