NEFU 118 n!后面有多少个0(数论)

来源:互联网 发布:为什么mac没有flash 编辑:程序博客网 时间:2024/04/28 16:14

n!后面有多少个0

http://acm.nefu.edu.cn/test/problemshow.php?problem_id=118

Time Limit 1000ms

Memory Limit 65536K

description

从输入中读取一个数n,求出n!中末尾0的个数。

input

输入有若干行。第一行上有一个整数m,指明接下来的数字的个数。然后是m行,每一行包含一个确定的正整数n,1<=n<=1000000000。

output

对输入行中的每一个数据n,输出一行,其内容是n!中末尾0的个数。

sample_input

331001024

sample_output

024253

直接套公式,注意到2的因子数要比5多,所以算5就行。

完整代码:
/*1ms,696KB*/#include <cstdio>using namespace std;int main(void){int n, m;scanf("%d", &m);while (m--){scanf("%d", &n);int t = 5, cnt = 0;while (t <= n){cnt += n / t;t *= 5;}printf("%d\n", cnt);}return 0;}