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的个数。
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;
}