P5737 【深基7.例3】闰年展示

1.题目介绍

【深基7.例3】闰年展示

题目描述

输入 \(x,y\),输出 \([x,y]\) 区间中闰年个数,并在下一行输出所有闰年年份数字,使用空格隔开。

输入格式

输入两个正整数 \(x,y\),以空格隔开。

输出格式

第一行输出一个正整数,表示 \([x,y]\) 区间中闰年个数。

第二行输出若干个正整数,按照年份单调递增的顺序输出所有闰年年份数字。

样例 #1

样例输入 #1

1989 2001

样例输出 #1

3
1992 1996 2000

提示

数据保证,\(1582\le x < y \le 3000\)

2.题解

2.1 子程序

思路

知道怎么求闰年就行了

代码

#include<bits/stdc++.h>
using namespace std;
bool is_leap(int year){
	// 闰年的判断规则:
    // 1. 年份能被4整除,但不能被100整除,或者
    // 2. 年份能被400整除
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int main(){
	int x, y;
	cin >> x >> y;
	vector<int> arr;
	for (int i = x; i <= y; i++){
		if(is_leap(i)) arr.push_back(i); 
	}
	cout << arr.size() << endl;
	for(auto it = arr.begin(); it != arr.end(); it++){
		cout << *it << ' ';
	}
}
posted @ 2024-01-26 22:03  DawnTraveler  阅读(165)  评论(0)    收藏  举报