您的位置:首页 > 其它

阶乘 hdu 1124 (Factorial)

2016-08-23 19:55 447 查看
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.

 
[align=left]Input[/align]
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.

 
[align=left]Output[/align]
For every number N, output a single line containing the single non-negative integer Z(N).

 
[align=left]Sample Input[/align]

6
3
60
100
1024
23456
8735373

 
[align=left]Sample Output[/align]

0
14
24
253
5861
2183837

 题意:给出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: