版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33531813/article/details/79516258 </div><div id="content_views" class="markdown_views"><!-- flowchart 箭头图标 勿删 --><svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg><h2 id="蓝桥杯历届试题日期问题">蓝桥杯历届试题——日期问题</h2>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
bool isLeapYear(int year)
{if ((year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) return true ; return false ;
}
bool isLegal (int yy, int mm, int dd)
{if (mm > 12 || mm <= 0 ) return false ; if (dd >31 || dd <= 0 ) return false ; if (dd <29 ) return true ; else {if (dd == 31 && (mm == 2 || mm == 4 || mm == 6 || mm ==9 || mm == 11 )) return false ; if (dd == 30 && mm == 2 ) return false ;if (dd == 29 && mm == 2 ) {int year = (yy < 60 )? 2000 + yy : 1900 + yy;if (!(isLeapYear(year))) return false ;}} return true ;
}struct Date
{int date [3 ]; bool operator < (const struct Date dt) const{for (int i = 0 ; i < 3 ; i++){if (date [i] > dt.date [i]) return false ;else if (date [i] < dt.date [i]) return true ;}return false ;}
}legalDate[3 ]; int sum = 0 ;
void addLegalDate(int y, int m, int d)
{int year = (y < 60 )? 2000 + y : 1900 + y;for (int i = 0 ; i < sum ; i++){if (legalDate[i].date [0 ] == year && legalDate[i].date [1 ] == m && legalDate[i].date [2 ] == d)return ;}legalDate[sum ].date [0 ] = year;legalDate[sum ].date [1 ] = m;legalDate[sum ].date [2 ] = d;sum ++;
}int main()
{int date [3 ]; scanf("%d/%d/%d" , &date [0 ], &date [1 ], &date [2 ]); if (isLegal(date [0 ], date [1 ], date [2 ])){addLegalDate(date [0 ], date [1 ], date [2 ]);}if (isLegal(date [2 ], date [0 ], date [1 ])){addLegalDate(date [2 ], date [0 ], date [1 ]);}if (isLegal(date [2 ], date [1 ], date [0 ])){addLegalDate(date [2 ], date [1 ], date [0 ]);}sort(legalDate, legalDate + sum );for (int i = 0 ; i < sum ; i++){printf("%d-%02d-%02d\n" , legalDate[i].date [0 ], legalDate[i].date [1 ], legalDate[i].date [2 ]);}return 0 ;
}
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet"></div>