Factorial

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值