Factorial
Time Limit: 1500MS   Memory Limit: 65536K
Total Submissions: 15137   Accepted: 9349

Description

The most important part of a GSM network is so called Base Transceiver Station (BTS). These transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified
view). Of course, BTSes need some attention and technicians need to check their function periodically. 



ACM technicians faced a very interesting problem recently. Given a set of BTSes to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. Programmers have spent several months studying
this problem but with no results. They were unable to find the solution fast enough. After a long time, one of the programmers found this problem in a conference article. Unfortunately, he found that the problem is so called "Travelling Salesman Problem" and
it is very hard to solve. If we have N BTSes to be visited, we can visit them in any order, giving us N! possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4....N. The number is very high
even for a relatively small N. 



The programmers understood they had no chance to solve the problem. But because they have already received the research grant from the government, they needed to continue with their studies and produce at least some results. So they started to study behaviour
of the factorial function. 



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

题意是求一个数阶乘的末尾0的个数,相乘能在末尾多产生0的,只能是乘以10,即因子中含有2和5的,因为偶数的数量远远大于5的数量,所以这个题目就是要求一个数有多少个5,有多少个25,有多少个125。。。。

代码:

#include <iostream>
using namespace std; int main()
{
int Test,n,result;
cin >> Test; while (Test--)
{
cin >> n;
result = 0; while (n > 0)
{
result += n / 5;
n = n / 5;
}
cout << result << endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1401:Factorial 求一个数阶乘的末尾0的个数的更多相关文章

  1. 计算阶乘n!末尾0的个数

    一.问题描述 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数.例如: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= ...

  2. 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置

    出题:不同大小烙饼的排序问题:对于N块大小不一的烙饼,上下累在一起,由于一只手托着所有的饼,所以仅有一只手可以翻转饼(假设手足够大可以翻转任意块数的 饼),规定所有的大饼都出现在小饼的下面则说明已经排 ...

  3. N的阶乘末尾0的个数和其二进制表示中最后位1的位置

    问题一解法:     我们知道求N的阶乘结果末尾0的个数也就是说我们在从1做到N的乘法的时候里面产生了多少个10, 我们可以这样分解,也就是将从0到N的数分解成因式,再将这些因式相乘,那么里面有多少个 ...

  4. Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)

    一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...

  5. Algorithm --> 求阶乘末尾0的个数

    求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...

  6. 求N的阶乘N!中末尾0的个数

    求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...

  7. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  8. 求一个数的阶乘在 m 进制下末尾 0 的个数

    题意 : 求一个数 n 的阶层在 m 进制下末尾 0 的个数 思路分析 : 如果是 10 进制地话我们是很容易知道怎么做的,数一下其对 5 约数地个数即可,但是换成 m 进制的话就需要先将 m 分解质 ...

  9. LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...

随机推荐

  1. SpringIOC初始化过程源码跟踪及学习

    Spring版本 4.3.2,ssm框架 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 web.xml <!--配置获取项目的根路径,java类中使用System.getProperty ...

  2. SpringBoot-属性文件properties形式

    SpringBoot-属性文件properties形式 上述使用JavaBean的配置可以实现数据源的配置,但是如果配置文件中的内容需要被多次调用就没那么方便了,所以我们学习新的方法,将Propert ...

  3. MYSQL登录及常用命令

    1.mysql服务的启动和停止 mysql> net stop mysql mysql> net start mysql 2.登陆mysql mysql> 键入命令mysql -ur ...

  4. ACM-吴奶奶买鱼

    题目描述:吴奶奶买鱼   吴奶奶有个可爱的外孙女——琪琪,她很喜欢小动物,尤其喜欢养鱼.为了让小孙女养到漂亮的小鱼,吴奶奶一大早就到花鸟鱼虫市场买鱼.这个市场可真大,里面有各种各样的宠物,就连宠物鱼都 ...

  5. win下的常用8个命令

    windows下常用的几个指令 一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...

  6. UVA - 10891 Game of Sum (区间dp)

    题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...

  7. centos下安装tomcat8.

    一.tomcat安装之前,首先安装java jdk,所以手首先将相关安装包都下载传到虚拟机上 1.下载java的linux----jdk1.8安装 到官网上下载与本虚拟机版本位相适应的位节数的Linu ...

  8. cf 782# A.Andryusha and Socks B.The Meeting Place Cannot Be Changed C.Andryusha and Colored Balloons

    看来快掉到灰名的蒟蒻涨rating也快... A题模拟一下就好(一开始还sb,, #include<bits/stdc++.h> #define LL long long using na ...

  9. Go语言 一维数组的使用

    程序源码 package main import ( "fmt" // 导入 fmt 包,打印字符串是需要用到 ) func main() { // 声明 main 主函数 var ...

  10. Spring配置数据源的三种方法

    前言:今天接触新项目发现用的是JNDI配置数据源,用度娘倒腾了一会也没弄好,只好用平常用的方法,结果发现BasicDataSource和DriverManagerDataSource也是不同的,所以记 ...