阶乘 hdu 1124 (Factorial)

来源:互联网 发布:matlab矩阵逻辑运算 编辑:程序博客网 时间:2024/04/28 01:39

Factorial


Problem Description


For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1<N2, then Z(N1) <= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
 
Input
There is a single positive integer T on the first line of input. It stands for the number of numbers to follow. Then there is T lines, each containing exactly one positive integer number N, 1 <= N <= 1000000000.
 
Output
For every number N, output a single line containing the single non-negative integer Z(N).
 
Sample Input
63601001024234568735373
 
Sample Output
0142425358612183837
 题意:给出n,求n的阶乘的末尾有多少0.
 题解:由算数基本定理可:N!可划分为 质因数相乘的形式  N!=2^a*3^b*5^c*7^d........

因为只有2*5 才会出现 0   又因为2的数量肯定比5的多  所以计算阶乘中5的数量就可以得到该阶乘后有几个0。

50/5=10  10/5=2  所以50!后有10+2=12个0。

#include<cstdio>#include<cmath>using namespace std;int cnt;int solve(int n){cnt=0;while (n){cnt+=n/5;n/=5;}return cnt;}int main(){int n, t;scanf ("%d",&t);while (t--){scanf ("%d",&n);solve(n);printf ("%d\n",cnt);}return 0;} 



0 0