'commit'
This commit is contained in:
41
TangDou/LanQiaoBei/LanQiaoBeiWhitePaper/8_DiffDate.cpp
Normal file
41
TangDou/LanQiaoBei/LanQiaoBeiWhitePaper/8_DiffDate.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Date {
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
};
|
||||
int months_1[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
int months_2[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
bool LeapYear(int year) {
|
||||
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
|
||||
}
|
||||
|
||||
//计算到元年元月元日的差
|
||||
int getAbsDay(Date x) {
|
||||
int n = 0;
|
||||
//以往年有多少天
|
||||
for (int i = 1900; i < x.year; i++)
|
||||
n += (LeapYear(i)) ? 366 : 365;
|
||||
//月-->天数+
|
||||
for (int i = 1; i < x.month; i++)
|
||||
n += LeapYear(x.year) ? months_2[i] : months_1[i];
|
||||
//天数
|
||||
n += x.day;
|
||||
return n;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//计算两个日期对象的差
|
||||
Date a = {2021, 8, 26}; //周四
|
||||
Date b = {2021, 10, 11}; //?
|
||||
|
||||
//计算到元年元月元日的差
|
||||
int n = getAbsDay(b) - getAbsDay(a);
|
||||
//输出
|
||||
printf("%d *%d", n, (n + 4) % 7);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user