AD 2020
先预处理年份
再处理边边角角
模拟难度+++
很细的一个题
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int num[10005], y1, m1, d1, y2, m2, d2;
int checkY(int y) {
if (y / 1000 == 2 && y / 100 % 10 == 0 && y / 10 % 10 == 2) return 1;
if (y / 100 % 10 == 2 && y / 10 % 10 == 0 && y % 10 == 2) return 1;
return 0;
}
int checkLeap(int y) {return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;}
void init() {
for (int i = 2000; i <= 9999; ++i)
{
if (checkY(i)) num[i] = 365 + checkLeap(i);
else if (i % 10 == 2) num[i] = 29 + checkLeap(i);
else num[i] = 2;
num[i] += num[i - 1];
}
}
int add(int y, int m, int d)
{
int res = 0;
if (checkY(y)) {
for (int i = 1; i < m; ++i) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10) res += 31;
else if (i == 2) res += 28 + checkLeap(y);
else res += 30;
}
res += d;
return res;
}
if (y % 10 == 2) {
if (m > 2) res += 28 + checkLeap(y);
if (m == 2) res += d;
if (m == 12) res += (d >= 2);
return res;
}
if (m > 2) res += 1;
if (m == 2) res += (d >= 2);
if (m == 12) res += (d >= 2);
return res;
}
void solve() {
int ans = 0;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
ans = num[y2 - 1] - num[y1 - 1];
// 为什么要加上 y1这年一直到y2的上一年呢
// 因为可以用add函数求出 从每一年开始到这个日期中 存在202的日子
// 都是求上半部分,具有相似性
ans += add(y2, m2, d2) - add(y1, m1, d1);
if (checkY(y1) || (y1 % 10 == 2 && m1 == 2) || (m1 == 2 && d1 == 2) || (m1 == 12 && d1 == 2)) ans += 1;
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
init();
int _T;
cin >> _T;
while (_T--) solve();
return 0;
}
4万+

被折叠的 条评论
为什么被折叠?



