70分的需要大佬帮忙

P2010 [NOIP2016 普及组] 回文日期

a1766465080 @ 2023-08-25 09:37:48

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool isRunnian(string date) {
    int nums = 0;
    for (int i = 3; i >= 0; --i) {
        nums = nums * 10 + (date[i] - '0');
    }
    if (nums % 400 == 0 || (nums % 4 == 0 && nums % 100 != 0)) {
        return true;
    } else {
        return false;
    }
}

bool isValid(string year1, string year2, string monday1, string monday2, string now) {
    // string monday = reverse(now.begin(), now.end());
    string monday;
    for (int i = 3; i >= 0; --i) {
        monday.push_back(now[i]);
    }
    if (monday > "1231" || monday < "0100") {
        return false;
    }
    string month(monday, 2);
    string day = monday.substr(2, 2);

    if (month[0] == '0') {
        switch (month[1]) {
            case '1' : 
            case '3' :
            case '5' :
            case '7' :
            case '8' : 
                if (day > "31") {
                    return false;
                }
                break;
            case '4' :
            case '6' :
            case '9' :
                if (day > "30") {
                    return false;
                }
                break;
        }
        if (isRunnian(now) && month[1] == '2') {
            if (day > "29") {
                return false;
            }
        }
        if (!isRunnian(now) && month[1] == '2') {
            if (day > "28") {
                return false;
            }
        }
    } else {
        switch (month[1]) {
            case '0' :
            case '2' :
                if (day > "31") {
                    return false;
                }
                break;
            case '1' :
                if (day > "30") {
                    return false;
                }
                break;

        }
    }

    if (now == year1) {
        if (monday < monday1) {
            return false;
        }
    }
    if (now == year2) {
        if (monday > monday2) {
            return false;
        }
    }
    return true;
}

int main() {
    string date1, date2;
    cin>>date1>>date2;
    string year1;
    string year2;
    for (int i = 0; i < 4; ++i) {
        year1.push_back(date1[i]);
        year2.push_back(date2[i]);
    }
    // cout<<year1<<"\n";
    // string year2(date2, 4);
    // cout<<year2<<"\n";
    string monday1 = date1.substr(4, 4);
    string monday2 = date2.substr(4, 4);
    int result = 0;
    // cout<<monday1<<"\n";
    // cout<<monday2<<"\n";
    string now(year1);
    while (now <= year2) {
        if (isValid(year1, year2, monday1, monday2, now)) {
            cout<<now<<"\n";
            result++;
        }
        // int flag = 0;
        if (now[3] == '9') {
            now[3] = '0';
            if (now[2] == '9') {
                now[2] = '0';
                if (now[1] == '9') {
                    now[1] = '0';
                    now[0] += 1;
                } else {
                    now[1] += 1;
                }
            } else {
                now[2] += 1;
            }
        } else {
            now[3] += 1;
        }
    }
    cout<<result;

    return 0;
}

by __hjwucj__ @ 2023-08-25 09:41:37

#include <bits/stdc++.h>
#define int long long
using namespace std;
int x,y,ans;
int dx (int k)
{
    int ans=0;
    while (k)
    {
        ans=ans*10+k%10;
        k/=10;
    }
    return ans;
}
bool hw (int x)
{
    int s=dx (x);
    if (x==s) return true;
        else return false;
}
bool pd (int x)
{
    int l=x%10000/100,t=x%10000%100;
    if (l>12||l==0) return false;
    int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    if ((x/10000%4==0&&x/10000%100!=0)||x/10000%400==0) a[2]=29;
    if (t>a[l]||t==0) return false;
    return true;
}
signed main ()
{
    cin>>x>>y;
    for (int i=x/10000;i<=y/10000;i++)
        for (int j=101;j<=1231;j++)
            if (hw (i*10000+j)&&pd (i*10000+j)&&i*10000+j<=y) ans++/*,cout<<i*10000+j<<endl*/;
                else if (i*10000+j>y) break;
    cout<<ans<<endl;
    return 0;
}

by wujiawei36 @ 2023-08-30 12:40:38

#include<bits/stdc++.h>
using namespace std;
int month[]= {-1,31,28,31,30,31,30,31,31,30,31,30,31};
bool hw(int a,int b,int c)
{
    if(a/1000==c%10 && a/100%10==c/10 && 
         a/10%10==b%10 && a%10==b/10)return true;
    return false;
}
int main()
{
    int date1,date2;
    cin>>date1>>date2;
    int a1=date1/10000,b1=date1/100%100,c1=date1%100;
    int a2=date2/10000,b2=date2/100%100,c2=date2%100;
    int ans=0;
    for(int a=a1; a<=a2; a++)
    {
        if(a%400==0||(a%100!=0&&a%4==0))month[2]=29;
        else month[2]=28;
        for(int b=b1; b<=12; b++)
        {
            if(b>b2&&a==a2)break;
            for(int c=c1; c<=month[b]; c++)
            {
                if(c>c2&&a==a2&&b==b2)break;
                if(hw(a,b,c))
                {
                    ans++;
                }
            }
        }
    }
    cout<<ans;
    return 0;
}

|