poj1401

博客围绕POJ-1401问题展开,该问题定义函数Z,求正整数N的阶乘N!末尾零的个数。解题思路是统计1到n中5的倍数个数,不同次幂的5倍数计算次数不同。还给出了算法实现源代码的转载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.链接地址

https://vjudge.net/problem/POJ-1401

2.问题描述

 For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1 < N2, then Z(N1) <= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently. 

输入样例

6
3
60
100
1024
23456
8735373

输出样例

0
14
24
253
5861
2183837

3.解题思路

 这道题的思路是从1到n的数中有几个5的倍数,如果是10、20、30等等这些数,因子中只有一个5,所以算一次。如果是25,50,75等等这些数,因子中有5的平方,所以算两次。依此规律,125,625等等同样可以这样算。

4.算法实现源代码

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int s[20];
int main()
{
    int i,j,n,k,l;
    int ss,ans;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&k);
        ans=0;
        ss=5;
        while(ss<=k)
        {
            ans+=k/ss;
            ss*=5;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/KasenBob/p/11163931.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值