日期问题

日期问题

需要考虑的问题

  1. 是否是闰年来确定二月的天数
  2. 日期是否合理(month在112 day在128/29/30/31(根据月份来判断))
  3. 日期的输出顺序是否合理
  4. 日期相同时只需要输出一次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>

using namespace std;

string c;

struct node{
string yy,mm,dd;
}d[10];

bool cmd(const node& a, const node& b){
if(a.yy != b.yy) return a.yy < b.yy;
else if(a.mm != b.mm) return a.mm < b.mm;
else{
a.dd < b.dd;
}
}

int main(){
cin >> c;
string yy,mm,dd;
int cnt = 0;
int day[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

for(int i=0;i<3;i++){
string str = c;
bool flag = false;
if(i == 0){
yy = str.substr(0,2); mm = str.substr(3,2); dd = str.substr(6,2);
}
else if(i == 1){
yy = str.substr(6,2); mm = str.substr(0,2); dd = str.substr(3,2);
}
else{
yy = str.substr(6,2); mm = str.substr(3,2); dd = str.substr(0,2);
}

if(yy <= "59"){
yy = "20" + yy;
}
else{
yy = "19" + yy;
}

string t = yy;
int a = stoi(t);

if(mm <= "00" || mm > "12"){
continue;
}
else if(mm == "02"){
if(a % 400 == 0 || (a % 4 == 0 && a % 100 !=0 )){
if(dd > "29" || dd <= "00" ) continue;
}
else{
if(dd > "28" || dd <= "00" ) continue;
}
}
else if(mm == "01" || mm == "03" || mm == "05" || mm == "07" || mm == "08" || mm == "10" || mm == "12"){
if(dd <= "00" || dd > "31"){
continue;
}
}
else{
if(dd <= "00" || dd > "30"){
continue;
}
}
for(int i=0;i<cnt;i++){
if(yy == d[i].yy && mm == d[i].mm && dd == d[i].dd) flag = true;
}
if(flag) continue;
//这个地方要用cnt,不能用i,因为i一直在循环中增加,但放入结构体数组中的数只有满足条件才会放入
d[cnt] = {yy,mm,dd};
cnt ++;
}
sort(d,d+cnt,cmd);
for(int i=0;i<cnt;i++){
cout << d[i].yy << "-" << d[i].mm << "-" << d[i].dd << "\n";
}
return 0;
}


日期问题
https://cs-lb.github.io/2024/04/10/algorithm_know/日期问题/
作者
Liu Bo
发布于
2024年4月10日
许可协议