求助,70分

P2010 [NOIP2016 普及组] 回文日期

ZZJM @ 2023-07-18 17:54:19

#include<iostream>
#include<string>
using namespace std;

bool isPalindrome(string str)//判断是否是回文数
{
    int left = 0;
    int right = str.size() - 1;
    while (left < right)
    {
        if (str[left++] != str[right--])
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int day1[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    int count = 0;
    int des1, des2;
    cin >> des1 >> des2;

    for (int i = des1; i <= des2; i++)
    {
        int year = i / 10000;
        int month = (i / 100) % 100;
        int day = i % 100;
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        {
            day1[1]++;
        }
        if (month > 12 || month == 0)
        {
            continue;
        }
        if (day1[month-1] < day || day == 0)
        {
            continue;
        }
        string str = to_string(i);
        if (isPalindrome(str))
        {
            count++;
        }
        day1[1]--;
    }

    cout << count;
    return 0;
}

by ljc888 @ 2023-07-18 18:03:23

#include<iostream>
#include<string>
using namespace std;

bool isPalindrome(string str)//判断是否是回文数
{
    int left = 0;
    int right = str.size() - 1;
    while (left < right)
    {
        if (str[left++] != str[right--])
        {
            return false;
        }
    }
    return true;
}
int main(){
    int day1[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    int count = 0;
    int des1, des2;
    cin >> des1 >> des2;

    for (int i = des1; i <= des2; i++){
        int year = i / 10000;
        int month = (i / 100) % 100;
        int day = i % 100;
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
            day1[1]=29;
        }else day1[1]=28;
        if (month > 12 || month == 0)
        {
            continue;
        }
        if (day1[month-1] < day || day == 0)
        {
            continue;
        }
        string str = to_string(i);
        if (isPalindrome(str))
        {
            count++;
        }
        day1[1]--;
        //cout << 
    }

    cout << count;
    return 0;
}

by ljc888 @ 2023-07-18 18:06:49

你只需要把:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        {
            day1[1]++;
        }

改成:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
            day1[1]=29;
        }else day1[1]=28;

就行了。

@ZZJM 如果对你有帮助你可以关注一下,谢谢!


by ZZJM @ 2023-07-18 18:17:30

@ljc888 谢谢大佬,关注了


|