70分的需要大佬帮忙
查看原帖
70分的需要大佬帮忙
976019
a1766465080楼主2023/8/25 09:37
#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;
}
2023/8/25 09:37
加载中...