AD 2020

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值