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. vs2010编译C++ 运算符

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  2. iOS dismissViewControllerAnimated:completion:使用方法

    我们都知道dismissViewControllerAnimated:completion:方法是针对被present出来的控制器的,一般我们这样使用:在一个控制器中present另外一个控制器A,然 ...

  3. 安装ruby的一些坑

    之前一直下载不下来.是因为需要翻墙.

  4. @Autowired 和 @ Resource 的区别

    转 都是用来装配Bean的注解.都可以写在字段上,或写在setter方法上. @Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以 ...

  5. 数学软件实训1-MATLAB程序设计及应用初步

    数学软件实训任务一 一 题目:MATLAB程序设计及应用初步 二 目的:掌握MATLAB程序设计的基本方法,会利用MATLAB程序设计思想编程处理一些简单问题. 三 要求: 1 掌握控制流的基本语法结 ...

  6. Eclipse打包Android项目时用到proguard.cfg后,出现的Warning:can't find referenced class问题的解决方案

    原文地址:http://blog.csdn.net/u_xtian/article/details/7495023 这个看似简单的问题困扰了我好久了,我已经google了很多相关的信息了,但是在我看来 ...

  7. 032、Java中判断某一个数字是奇数还是偶数

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  8. 如何让你的学术Essay写作看起来更高级?

    学术风格不仅是使用某些特定的词汇或者特定的表达方式.学术风格还会涉及到你的思考方式.推理方式以及你如何利用某个领域内已知的发现.学术风格是英国大学学习的核心.所以在平时的学习中,英国环球论文小编建议大 ...

  9. redis学习(五)

    一.Redis 发布订阅 1.Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 2.Redis 客户端可以订阅任意数量的频道. 比如你订阅 ...

  10. node - 获取 token

     String(req.headers.authorization || '').split(' ').pop()