70分求助dalao

P2010 [NOIP2016 普及组] 回文日期

LYQ310 @ 2023-03-25 10:03:47

思路已经很明晰了,枚举年份

#include<iostream>
#include<cstdio>
using namespace std;
int g[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool run(int a){
    if(a%400==0) return 1;
    if(a%4==0&&a%100!=0) return 1;
    return 0;
}
int main()
{
    //freopen("num.in","r",stdin);
    //freopen("num.out","w",stdout);
    int ans=0;
    string d1,d2;
    int yo=0,yt=0;
    cin>>d1;cin>>d2;
    for(int i=0;i<4;i++){
        yo=yo*10+d1[i]-'0';
        yt=yt*10+d2[i]-'0';
    }
    for(int i=yo;i<yt;i++){
        int m=i%10*10+i%100/10;
        int n=i%100/100*10+i/1000;
        if(run(i)) g[2]=29;
        if(m<=12&&n<=g[m]) ans++;
        g[2]=28;
    }
    if(d2[3]*10+d2[2]<=d2[4]*10+d2[5]&&d2[1]*10+d2[0]<=d2[6]*10+d2[7]) ans++;
    cout<<ans<<endl;
    //fclose(stdin);fclose(stdout);
    return 0;
}

by __zhuruirong__ @ 2023-03-28 19:21:58

#include <bits/stdc++.h>
using namespace std;

int months[13] = {
    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
}, ans, date1, date2;

int is_add(int month)
{
    return month == 2;
}

int is_leap(int year, int month)
{
    if((year % 4 == 0 and year % 100 != 0) or year % 400 == 0)
        return months[month] + is_add(month);
    return months[month];
}

bool is_elig(int date)
{
    int year = date / 10000;
    int month = date % 10000 / 100;
    int day = date % 100;
    if(month < 1 or month > 12) return false;
    if(day > is_leap(year, month)) return false;
    return true;
}

int main()
{
    cin >> date1 >> date2;
    for(int i = 1000; i <= 9999; i++)
    {
        string a = to_string(i);
        string b(a.rbegin(), a.rend());
        int date = stoi(a+b);
        if(date >= date1 and date <= date2 and is_elig(date))
            ans++;
    }
    cout << ans << endl;
}

by FlyCN87 @ 2023-06-26 21:38:42

建议换一个思路,枚举回文数,再看这个回文数是否合法,具体可以看题解


|