求助,超了一个点,怎么优化

P2010 [NOIP2016 普及组] 回文日期

91xrz @ 2023-03-20 21:34:17

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n, m,ans;
string c;
int x[13][2]{ {0,0}, {31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31} };
bool check_year(int x)
{
    return (x % 4 == 0 && x % 100 != 0 || x % 400 == 0);
}

bool check(int a)
{

    c.clear();
    while (a)
    {
        int t = a % 10;
        c += (t + '0');
        a /= 10;

    }
    for (int i = 0; i < c.size() / 2; i++)
    {
        if (c[i] != c[c.size() - 1 - i])
            return  false;

    }

    return true;
}

void sub(int a, int b)
{       
    if (a > b)
        sub(b, a);
    else
    {
        int year1 = a / 10000, year2 = b / 10000;
        int month1 = a / 100 % 100, month2 = b / 100 % 100;
        int day1 = a % 100, day2 = b % 100;

        while (year1<year2||month1<month2||day1<day2)
        {

                day1++;
                if (day1 == x[month1][check_year(year1)] + 1)
                {
                    month1++;
                    day1 = 1;
                }
            if (month1 == 13)
            {
                year1++;
                month1 = 1;
            }

                int newday = ((year1 * 100 + month1) * 100) + day1;
                if (check(newday))
                    ans++;

        }

    }

}

int main()
{
    int a, b;
    cin >> a>>b;

    if (a == b&&check(a))
        ans++;
    sub(a, b);

    cout << ans;

}

by ZYL39 @ 2023-03-23 20:29:46

@91xrz 开o2优化


|