链接
针对日期问题,可以定义一个数组去记录每个月的天数情况,方便计算,但是要注意闰年平年的差异性.
Code:
#include <bits/stdc++.h>
using ll = long long;
int day_count[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//存储每个月有多少天
bool is(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
} else if(year % 400 == 0) {
return true;
} else {
return false;
}
//对于闰年的判断
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int ans = 0, year = 2000, month = 5, day = 4;
if (is(year)) {
day_count[2]++;
//年份如果是闰年,二月份的天数为29天
}
for (int i = 1; i < month; i++) {
ans += day_count[i];
}
ans += day;
std::cout << ans;
return 0;
}