Factorial
Time Limit: 1500MS   Memory Limit: 65536K
Total Submissions: 13993   Accepted: 8678

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

题目:http://poj.org/problem?id=1401

题目是求N!中末尾0的个数。

这道题,是我曾经在经典例题100道中看到的,做出来的。可是,发现理解错了。(感谢一位网友的提醒啊!)

所以,又一次做一下。发现poj上有这道题,正好能够拿来測试一下做的是否正确。

这道题做法,显然不能求出来阶乘再数0的个数。

所以,要换一个思路。

为什么会产生0呢?源于2x5,于是能够通过数有多少个(2,5)因子对来推断有多少个0.

我们又能够发现。5的个数永远会大于2的个数。

又能够简化为数5的个数来做。

当时,还非常年轻。做法让如今的我看,有点纠结啊。

当时做的是。从1到N(所输入的数)循环,看看能不能被5整除。若能整除,继续除5。直到不能整除为止。

这个做法显然答案正确,可是在这道题里,TLE必须的。。。

#include <stdio.h>
int main()
{
int i,N,k,t;
double num;
bool prime;
scanf("%d",&t);
while( t-- )
{
scanf("%d",&N);
k=0;
for(i=1;i<=N;i++) //查看有多少个5
{
num=i/5.0;
if(num-int(num)==0)
prime=true;
else
prime=false;
while(num>=1&&prime==true)
{
num/=5.0;
if(num-int(num)==0)
prime=true;
else
prime=false;
k+=1;
}
}
printf("%d\n",k);
}
return 0;
}

然后。如今想想,这道题,不须要这么麻烦啊。

数5的倍数,就能够了。

以698为例:

698/5=139.6→取整→139     (表示有139个数为5的倍数) sum=sum+139 (sum初始化为0)

139/5=27.8 →取整→27        (表示有27个数为25的倍数)sum=sum+27

(为什么25的倍数,仅仅加了一遍,不应该加 27*2的吗,25表示两个5?

由于,25的之前有一个5,在第一遍139个里面算过一次了。所以不须要加两遍。

27/5=5.4 → 取整→5             (表示有5个数为125的倍数)sum=sum+5

5/5=1 → 取整 → 1
     (表示有1个数为625的倍数) sum=sum+1

1/5=0  结束。

所以答案是   139+27+5+1=172

恩。所以程序,就非常easy了:

/**************************************
***************************************
* Author:Tree *
*From :http://blog.csdn.net/lttree *
* Title : Factorial *
*Source: poj 1401 *
* Hint : N!求0的个数 *
***************************************
**************************************/
// scanf 125MS, cin 422MS
#include <stdio.h>
int main()
{
// sum为答案。存储每次除5后的数(累加)
int t,n,sum;
scanf("%d",&t);
while( t-- )
{
sum=0;
scanf("%d",&n);
while( n )
{
n/=5;
sum+=n;
}
printf("%d\n",sum);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

ACM-简单的主题Factorial——poj1401的更多相关文章

  1. ROS笔记——创建简单的主题发布节点和主题订阅节点

    在安装好ROS后,接着学习如何创建节点和节点之间的通信方式,以一个简单的主题发布节点和主题订阅节点说明. 节点是连接ROS网络等可执行文件,是实现某些功能的软件包,也是一个主要计算执行的进程. 一.创 ...

  2. ACM-简单题之Factorial——poj1401

    转载请注明出处:http://blog.csdn.net/lttree Factorial Time Limit: 1500MS   Memory Limit: 65536K Total Submis ...

  3. ACM——简单排序

    简单选择排序 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:836            测试通过:259 描述 给定输入排序元素 ...

  4. RabbitMQ简单应用の主题模式(topic)

    Topic exchange(主题转发器) 发送给主题转发器的消息不能是任意设置的选择键,必须是用小数点隔开的一系列的标识符.这些标识符可以是随意,但是通常跟消息的某些特性相关联.一些合法的路由选择键 ...

  5. hdu acm 简单暴力1004

    字符串匹配函数strcmp 直接使用来判断两字符串是否完全相等 用数组存每个单词的个数时  初始化为零就错 初始化为一时就正确  也不知道为什么

  6. 2道acm简单题(2013):1.(时分秒)时间相减;2.主持人和N-1个人玩游戏,每个人说出自己认识的人数,判断其中是否有人说谎。

    /*1.题目:输入一个数,代表要检测的例子的个数,每个例子中:输入两个时间(格式HH:MM : SS),前面时间减去后面时间,输出在时钟上显示的时间,格式一样,如果是以为数字的前面补零.*//**思路 ...

  7. 3道acm简单题(2011):1.判断是否能组成三角形;2.判断打鱼还是晒网;3.判断丑数。

    //1.输入三个正整数A.B.C,判断这三个数能不能构成一个三角形.//思路:最小的两边之和是否是大于第三边#include<iostream>#include<algorithm& ...

  8. 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;

    //第一题是猜数字的游戏.//题目:随即产生一个3位的正整数,让你进行猜数字,//如果猜小了,输出:"猜小了,请继续".//如果猜大了,输出:"猜大了,请继续" ...

  9. wordpress学习四: 一个简单的自定义主题

    在学习三里分析了自带的一个例子,本节我们就自己仿照他做个简单的吧,重点是调用wordpress封装好的函数和类,css和html可以稍好在调整. 将wp带的例子复制一份处理,重新名个名字. 清空ind ...

随机推荐

  1. 致网友Wonderfei的一封信(怎样选择自己主动化框架的几点拙见)

    注:本来这封信要发给Wonerfei网友的,可是由于每次仅仅能发200字,所以干脆贴到博客上,叫Wonderfei同学到这上面来看,也算是我自己的一个暂时总结吧.同一时候也希望大家给予Wonderfe ...

  2. Unable to load Virtual engine

    相关:GenyMotion自带VirtualBox 解决办法:首先确认genymotion及virtualBox正确安装,然后右键点击启动程序选属性,在兼容性里选以兼容模式运行,同时勾选以管理员身份运 ...

  3. Gallatin(大陆版)Office365中Exchange Online混合部署功能已经能够使用了

    经过測试,Exchange混合部署已经能够使用了 前置条件: 本机至少须要一台Exchange Server 2013作为混合部署server 须要一个公网域名 domian.com,能够和内部域名不 ...

  4. php:的图形计算器的面向对象的版本武器2

    通过自带部分result.class.php分流,由于这三个类继承shape这个类,让我们来看看,面向对象的继承. shape.class.shape档 <?php abstract class ...

  5. list-style-type 去除li 前面的标记(小黑点)

    list-style-type 设置标记的样式(或者隐藏标记) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  6. R语言做文本挖掘 Part5情感分析

    Part5情感分析 这是本系列的最后一篇文章,该.事实上这种单一文本挖掘的每一个部分进行全部值获取水落石出细致的研究,0基础研究阶段.用R里面现成的算法,来实现自己的需求,当然还參考了众多网友的智慧结 ...

  7. asp.net学习之ado.net(无连接模式中的DataAdapter)

    原文:asp.net学习之ado.net(无连接模式中的DataAdapter) 在非连接模式下,主要讨论以下对象:DataAdapter.     DataAdpater的作用是在物理存储模式的数据 ...

  8. [Error]EOL while scanning string literal

    有一个经常性的工作项目.需要一天的一些表数据到外部接口,但最近总是异常.今天检查的原因. 第一本地和测试环境中测试程序是没有问题,有网络环境只会在日志中抛出一个异常.产生主要的例外是推定异常数据. , ...

  9. ABP依赖注入

    ABP依赖注入 点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之6.ABP依赖注入 ABP是“ASP.NET Boilerplate Project (ASP.N ...

  10. html浏览器兼容性 JavaScript语法

    1.      在FireFox中能够使用与HTML节点对象ID属性值同样的JS变量名称,可是IE中不行. 解决的方法:在命名上区分HTML节点对象ID属性值和JS变量 2.      IE不支持JS ...