题目
C++代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char str[9];
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int date[5];
bool check(int year, int month, int day)
{
if(month == 0 || month > 12) return false;
if(day == 0 || month != 2 && day > days[month]) return false;
bool leap = year % 4 == 0 && year % 100 || year % 400 == 0;
if(month == 2 && day > 28 + leap) return false;
return true;
}
int main()
{
int x1 = 0, x2 = 0, x3 = 0;
cin >> str;
for(int i = 0; i < 9; i ++ )
{
if(i < 2) x1 = x1 * 10 + (str[i] - '0');
if(i == 3 || i == 4) x2 = x2 * 10 + (str[i] - '0');
if(i == 6 || i == 7) x3 = x3 * 10 + (str[i] - '0');
}
//年月日
int y = x1 >= 60 ? 19 : 20;
y = y * 100 + x1;
if(check(y, x2, x3)) date[0] = y * 10000 + x2 * 100 + x3;
else date[1] == -1;
//日月年
y = x3 >= 60 ? 19 : 20;
y = y * 100 + x3;
if(check(y, x2, x1)) date[1] = y * 10000 + x2 * 100 + x1;
else date[2] == -1;
//月日年
if(check(y, x1, x2)) date[2] = y * 10000 + x1 * 100 + x2;
else date[3] == -1;
sort(date, date + 3); //排序,按照日期从早到晚,即从小到大
for(int i = 0; i < 3; i ++ )
{
//如果有重复的
if(date[i] == date[i + 1]) continue;
if(date[i] != -1)
{
int d = date[i];
int year = d / 10000;
int month = d % 10000 / 100;
int day = d % 100;
//如果年份为0
if(year == 0) continue;
printf("%d-%02d-%02d\n", year, month, day);
}
}
return 0;
}
菜啊菜QWQ
互粉