蓝桥杯 PREV-39 日期问题

题目链接:

PREV-39 日期问题

思路:

1.将题目所给的三种格式都尝试一下;
2.注意排序输出;
3.算闰年遵守“四年一闰,百年不闰,四百再闰”的规则(闰年2月29天,平年2月28天);
4.注意计算得出的重复日期要删去;

代码:

#include<bits/stdc++.h>

using namespace std;

struct date { 
	int y, m, d;
	bool operator < (const date & a) const {
		if(y != a.y) return y < a.y;
		return m == a.m ? d < a.d : m < a.m;	
	}
};
vector<date> ans;
int num[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 

inline void check(int y, int m, int d) {
	if(y <= 59) y += 2000;
	else y += 1900;
	if(m < 1 || m > 12) return;
	if(y % 400 == 0 || (y % 4 == 0 && y % 100)) num[2] = 29;
	else num[2] = 28;
	if(d > 0 && d <= num[m]) ans.push_back(date{y, m, d});
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	int a, b, c;
	scanf("%d/%d/%d", &a, &b, &c);
	check(a, b, c);
	check(c, a, b);
	check(c, b, a);
	sort(ans.begin(), ans.end());
	for(int i = 0; i < ans.size(); i++) {
		if(!i || ans[i - 1] < ans[i])
		printf("%d-%02d-%02d\n", ans[i].y, ans[i].m, ans[i].d);	
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值