蓝桥杯 - 历届试题 - 日期问题
生活随笔
收集整理的這篇文章主要介紹了
蓝桥杯 - 历届试题 - 日期问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權聲明:本文為博主原創文章,未經博主允許不得轉載。 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;//判斷 year 是否是閏年
//默認 year已經是大于零的數
bool isLeapYear(int year)
{if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return true; return false;
}//對傳來的3個參數是否能構成一個日期 yy-mm-dd
//規定:
// yy∈[00,59] yy = 20yy
// yy∈[60,99] yy = 19yy
// 即yy無需判斷,默認合法,但某種情況下需要判斷yy是閏年/平年。
// mm∈[01,12] 否則不合法
// dd∈[01,31] 否則不合法
// 特別地:
// dd∈[29,31] 需要根據月份、閏年/平年進行討論
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; // 日期小于29,必定合法else{// dd 為31的情況下, 2,4,6,9,11月是不可能的 if(dd == 31 && (mm == 2 || mm == 4 || mm == 6|| mm ==9 || mm == 11)) return false; // dd 為30的情況下,2月是不可能的 if(dd == 30 && mm == 2) return false;// dd 為29的情況下,2月的平年是不可能的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]; // 0-yy; 1-mm; 2-ddbool 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; //記錄實際共有幾個合法的日期 //把合法的日期加入legalDate
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]); //窮舉3種可能的情況 // 年/月/日 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
總結
以上是生活随笔為你收集整理的蓝桥杯 - 历届试题 - 日期问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用唤醒APP的方式
- 下一篇: f5+big+client+androi