Factorial
2014-04-25 14:27
253 查看
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 becausewe 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的个数。
算法思想来源:http://blog.163.com/soonhuisky@126/blog/static/1575917392010314024481/
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; }
相关文章推荐
- 动易2006序列号破解算法公布
- C#数据结构与算法揭秘二
- 浅析STL中的常用算法
- JavaScript 组件之旅(二)编码实现和算法
- java数据结构和算法学习之汉诺塔示例
- python计数排序和基数排序算法实例
- python基础教程之python消息摘要算法使用示例
- php的hash算法介绍
- 将15位身份证补全为18位身份证的算法示例详解
- python实现simhash算法实例
- C++算法系列之日历生成的算法代码
- 利用线性表的顺序结构求集合的并、交、差、补(C语言实现)
- 1 2 3 4 5 6 7 8 9 = 110的java实现
- Sedgewick之巨著《算法》,与高德纳TAOCP一脉相承
- 【代码】Pythonの代码片段
- STL中算法
- 数据结构&算法学习
- 算法的时间复杂度
- 算法导论:选择排序的原理与实现
- PHP实现四种常用的排序算法