题目链接:
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;
}