给出的一组时间可能是三种格式,由于年份只给出了最后两个数字,故在范围内每种格式的合法年份最多有两种,故可以将这可能的最多6个日期存入结构体中,重载小于符排序,最后从小到大依次输出合法的日期。这种方法构造的日期可能会出现重复,可使用map去重。
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
struct date{
int y, m, d;
bool operator <(const date &b) const{
return this->y < b.y || (this->y == b.y && this->m < b. m) || (this->y == b.y && this->m == b.m && this->d < b.d);
}
}d[6];
int idx;
int month[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(date a){
if(a.m == 2){
if(a.y % 4 == 0 && a.y % 400) {
if(a.d > 29) return false;
}
else if(a.d > 28) return false;
}
return (a.y >= 1960 && a.y <= 2059 && a.m && a.m <=12 && a.d && a.d <= month[a.m]);
}
map<date,int>mp;
int main(){
int a, b, c;
scanf("%d/%d/%d",&a, &b, &c);
d[idx++] = {a + 1900,b,c};
d[idx++] = {a + 2000,b,c};
d[idx++] = {c + 1900,a,b};
d[idx++] = {c + 2000,a,b};
d[idx++] = {c + 1900,b,a};
d[idx++] = {c + 2000,b,a};
sort(d, d + 6);
for(int i = 0; i < 6; i ++){
if(check(d[i]) && mp.find(d[i]) == mp.end()){
printf("%d-%02d-%02d\n",d[i].y, d[i].m, d[i].d);
mp[d[i]] = 1;
}
}
return 0;
}