您的位置:首页 > 其它

Factorial

2014-04-25 14:27 253 查看

POJ pid=1401

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).

题意大概是求N!的结果中末尾0的个数。
算法思想来源:http://blog.163.com/soonhuisky@126/blog/static/1575917392010314024481/

2*5=0,4*15=0。。。
此题关键是要想到5、15、25。。。这些数与其他偶数的乘积末尾一定为0,而且在1-N的范围内偶数的个数一定比5X多。同时又要看到25=5*5,125=5*5*5,4*25=100,8*125=1000,这些形式为5^n的数可以产生n个0(因为是n个5相乘),而其他的5X数则只能产生一个0。
AC代码:
#include<stdio.h>

int getZs(int lim){
int zes=0;
int div=5;
while(div<=lim){
zes+=lim/div;
div*=5;
}
return zes;
}
int main(){
int num=0;
int NG=0;
scanf("%d",&num);
while(num--){
scanf("%d",&NG);
printf("%d\n",getZs(NG));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 poj