AD 2020
发布日期:2021-05-06 17:49:03 浏览次数:19 分类:技术文章

本文共 1741 字,大约阅读时间需要 5 分钟。

先预处理年份
再处理边边角角
模拟难度+++
很细的一个题

#include 
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;}
上一篇:解方程
下一篇:并查集 + map (集合里元素的个数)

发表评论

最新留言

很好
[***.229.124.182]2025年03月22日 18时22分32秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章