码迷,mamicode.com
首页 > 其他好文 > 详细

51nod 1003 阶乘后面0的数量

时间:2014-11-30 23:25:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   os   sp   for   strong   on   

题目意思:

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1003

n的阶乘后面有多少个0?

6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1个0。
Input
一个数N(1 <= N <= 10^9)
Output
输出0的数量
Input 示例
5
Output 示例
1
题目分析:
对于本题,只需要知道在n!中,如果包含5的倍数,则末尾多一个0,如果包含5^2,末尾添加2个0,类推如果包含5^k,
末尾则加k个0。

/**
 *阶乘后面的0的个数,如果n!的中包含一个5,就有一个0
 *就有含有一个5就含有一个0,含有5^2就有2的0,依次同理
 *例如15%5==0,就算包含5
 */
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int a[13];a[0]=5;
    for(int i=1;i<13;i++){
        a[i]=a[i-1]*5;
    }
    //cout<<a[12]<<endl;
    int n;
    while(cin>>n){
        int cnt=0;
        for(int k=0;k<13;k++){
            for(int i=a[k];i<=n;i+=a[k]){
                cnt++;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}



51nod 1003 阶乘后面0的数量

标签:style   http   io   ar   os   sp   for   strong   on   

原文地址:http://blog.csdn.net/fool_ran/article/details/41628933

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!