POJ 1401 Factorial

http://poj.org/problem?id=1401

题意:

求n!的末尾有几个0。

思路:

0是由偶数与5的倍数的数相乘而得到的,因为偶数肯定是够多的,所以这里我们考虑5。

比如说,5能和偶数相乘可以得到1一个0,5^2和偶数相乘可以得到2个0,5^3和偶数相乘可以得到3个0......

所以个数就为n/5+n/25+n/125....

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 int n;
 9 
10 int main()
11 {
12     //freopen("D:\txt.txt", "r", stdin);
13     int T;
14     cin >> T;
15     while (T--)
16     {
17         cin >> n;
18         int ans = 0;
19         while (n > 1)
20         {
21             ans += n / 5;
22             n /= 5;
23         }
24         cout << ans << endl;
25     }
26 }
原文地址:https://www.cnblogs.com/zyb993963526/p/6576010.html