小白月赛5 D阶乘

链接:https://www.nowcoder.com/acm/contest/135/D
来源:牛客网
 

阶乘(factorial)

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

输入描述:

输入数据共一行,一个正整数n,意义如“问题描述”。

输出描述:

输出一行描述答案:

一个正整数k,表示S的末尾有k个0

示例1

输入

复制

10

输出

复制

7

说明

 

  鸣谢真·dalao Tyxao

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,sum,temp,cnt;
    scanf("%lld",&n);
    sum=0;
    cnt=0;
    for(ll i=1;i<=n;i++)
    {
        temp=i;
        while(temp%5==0)
        {
            cnt++;
            temp/=5;
        }
        sum+=cnt;
    }
    printf("%lld\n",sum);
}

就是利用了求阶乘末尾零的个数的思想,怎么样才不TLE,就是后面利用了前面的结果

猜你喜欢

转载自blog.csdn.net/qq_37891604/article/details/81162519