阶乘 hdu 1124 (Factorial)

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
  
  
6 3 60 100 1024 23456 8735373
 
Sample Output
  
  
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;
} 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值