求助!!第11个点WA了

P2010 [NOIP2016 普及组] 回文日期

tommyfj @ 2022-11-02 19:26:01

感觉没啥错。。

#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int n, m, ans = 0;
int mon1[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int mon2[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int check1(int x)
{
    int a, b, c, d, e;
    e = x % 10000;
    a = e % 10;
    b = e % 100 / 10;
    c = e / 100 % 10;
    d = e / 1000;
    //cout << a << " " << b << " " << c << " " << d << endl;
    //cout << x / 10000 << " " << a * 1000 + b * 100 + c * 10 + d << endl;
    if (x / 10000 == a * 1000 + b * 100 + c * 10 + d) return true;
    else return false;
}
int check2(int x)
{
    int nian, f = 0;
    nian = x / 10000;
    if (nian % 400 == 0 || (nian % 4 == 0 && nian % 100)) f = 1;
    int t, h;
    t = x % 10000 / 100;
    //cout << t << " ";
    h = x % 10000 % 100;
    //cout << h << " ";
    if (f == 1) 
    {
        if (t > 12 || t < 1) return false;
        else if (mon1[t] < h) return false;
    }
    else {
        if (t > 12 || t < 1) return false;
        else if (mon2[t] < h) return false;
    }
    return true;
}
signed main()
{
    cin >> n >> m;
    for (int i = n;i <= m;i += 10000)
    {
        int k;
        k = i / 10000;
        int a, b, c, d;
        a = k % 10;
        b = k % 100 / 10;
        c = k / 100 % 10;
        d = k / 1000;
        int z;
        z = k * 10000 + a * 1000 + b * 100 + c * 10 + d;
        if (check2(z)) ans ++;
    }
    cout << ans << endl;
    return 0;
}

by LYY_yyyy @ 2022-11-02 19:46:47

不需要mon2数组啊


by tommyfj @ 2022-11-02 20:08:36

@LYY_yyyy 额,平年二月份的天数和闰年二月份的天数不一样呀。


by LYY_yyyy @ 2022-11-02 20:27:00

@tommyfj 算一下就知道 92200229是合法的,所以直接置为29即可


by tommyfj @ 2022-11-02 20:28:45

@LYY_yyyy 哦,可是问题好像不在这?


by LegendaryGrandmaster @ 2022-11-02 21:50:15

#include<bits/stdc++.h>
using namespace std;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool ok(int y,int m,int d)
{
    if(y/1000!=d%10)return 0;
    if(y/100%10!=d/10)return 0;
    if(y%100/10!=m%10)return 0;
    if(y%10!=m/10)return 0;
    return 1;
}
int main()
{
    int y,m,d,Y,M,D,c=0,x,X;
    cin>>x>>X;
    y=x/10000;
    m=x%10000/100;
    d=x%100;
    Y=X/10000;
    M=X%10000/100;
    D=X%100;
    while(1){
        if(ok(y,m,d))c++;
        if(y==Y&&m==M&&d==D)break; 
        d++;
        if(y%4==0&&y%100!=0||y%400!=0)a[2]=29;else a[2]=28;
        if(d>a[m]){m++;d=1;}
        if(m==13){y++;m=1;}
    } 
    cout<<c;
}

by tommyfj @ 2022-11-02 23:08:05

@LYY_yyyy @acw_yxc

下了测试样例,发现还要判断它是不是再 [n,m] 范围内,以 AC ,谢谢!


by gjq0706 @ 2023-07-26 09:25:29

感谢,我也少了一个特殊判断


|