1 条题解
-
0
P76044 闰年统计 — 题解
专题: 函数
适用题目: 新生 OJ 本地题包中的 P76044,不要直接用于外站同名题。解题思路
把闰年判定写成独立函数,对
[L,R]依次调用;输出计数后再输出列表。复杂度
时间 ,额外空间 (C 版)。
易错点
世纪年份只有能被 400 整除才是闰年;没结果也要输出第二行。
C11 参考程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <limits.h> int is_leap(int y){return y%400==0 || (y%4==0 && y%100!=0);} int main(void) { int l,r,cnt=0;scanf("%d%d",&l,&r); for(int y=l;y<=r;y++)if(is_leap(y))cnt++; printf("%d\n",cnt); int first=1; for(int y=l;y<=r;y++)if(is_leap(y)){ if(!first)putchar(' '); printf("%d",y);first=0; } putchar('\n');return 0; }C++17 参考程序
#include <bits/stdc++.h> using namespace std; bool is_leap(int y) { return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int l, r; cin >> l >> r; vector<int> a; for (int y = l; y <= r; y++) if (is_leap(y)) a.push_back(y); cout << a.size() << '\n'; for (int i = 0; i < (int)a.size(); i++) cout << a[i] << (i + 1 == (int)a.size() ? '\n' : ' '); if (a.empty()) cout << '\n'; return 0; }
题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。
- 1
信息
- ID
- 1813
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 1
- 标签
- 提交数
- 0
- 已通过
- 0
- 上传者