文章

119

粉丝

68

获赞

86

访问

19.4k

头像
上交2018 Problem A 题解:可能你能想到第一个,但是不好想第二个
P1626 上海交通大学2018年机试题
发布于2025年2月8日 15:00
阅读数 85

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            int num = i;
            while (num % 5 == 0) {
                ans++;
                num /= 5;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

如果你学过奥数,可以知道,阶乘的0的个数,就是5因子的个数,一般来说,直接统计带来的效率提升已经非常高了,但是对于本题的数据量,他仍然是不够的,还需要继续优化

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        int ans = 0;
        while (n > 0) {
            n /= 5;
            ans += n;
        }
        cout << ans << endl;
    }
    return 0;
}

这里用数学公式计算得到结果

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发