美文网首页
笔试刷题-滴滴2018-06-14

笔试刷题-滴滴2018-06-14

作者: Dodo159753 | 来源:发表于2018-06-14 08:47 被阅读0次

    题目描述:

    
    /**
    输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2
    输入描述:
    输入为一行,n(1 ≤ n ≤ 1000)
    
    
    输出描述:
    输出一个整数,即题目所求
    
    输入例子1:
    10
    
    输出例子1:
    2
    */
    

    思路如下:

    算出n含有多少个2因子,n含有多少个5因子,取两者较小值即可

    代码如下:

    
    #include<stdio.h>
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int N, numOf2=0, numOf5=0;
        scanf("%d", &N);
        for(int i=1; i<=N; i++){
            int temp;
            temp=i;
            while(temp && temp%2==0){
                temp/=2;
                numOf2++;
            }
            temp=i;
            while(temp && temp%5==0){
                temp/=5;
                numOf5++;
            }
        }
        printf("%d", min(numOf2, numOf5));
        return 0;
    }
    
    
    

    相关文章

      网友评论

          本文标题:笔试刷题-滴滴2018-06-14

          本文链接:https://www.haomeiwen.com/subject/plseeftx.html