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 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因此求出 5 的个数即可。然后呢,问题就出来了,求一个阶乘里5的·个数。

除一次是5提供的0,除两次是25提供的0..........

代码:

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    long long int sum=0;
    if(n<=4)sum=0;
    else
    {
        while(n)
        {2
             sum+=(n/=5);
            // n
        }

    }
    printf("%lld\n",sum);
    return 0;
}

另一种:

#include <stdio.h>
#include <string.h>
#include <math.h>
#define maxn 1000000000
int k,n;
int main()
{
    int n;
    scanf("%d",&n);
    long long int i,cont=0,j;
    for( i=5,j=1;i<=n;i+=5,j++)
    {
        if(j%5==0)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        {
            int term=j;
            while(term%5==0)
            {
                term=term/5;
                cont++;
            }
        }
    }
    cont+=j-1;
    printf("%d\n",cont);
    return 0;
}


posted on 2017-10-24 21:29  zitian246  阅读(207)  评论(0)    收藏  举报