题解 P2010 【回文日期】

因为有8个字符,所以可得出每一年只有一个回文日期。

因此只要判断每一年就行了。

做法:

我们先把年倒过来,例如2018年就倒为8102,就得出8102就是回文日期的后四个字符,我们只要判断一下有没有这个月份和这个日期。

具体做法看AC代码

#include<bits/stdc++.h>
using namespace std;
int ans=0;
int montht[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//各个月份的天数
int main()
{
    int year,month,day;
    int year2,month2,day2;
    string s1,s2;
    cin>>s1>>s2;
    year=(s1[0]-'0')*1000+(s1[1]-'0')*100+(s1[2]-'0')*10+s1[3]-'0';//把起始年份单独提取出来
    year2=(s2[0]-'0')*1000+(s2[1]-'0')*100+(s2[2]-'0')*10+s2[3]-'0';//把终止年份单独提取出来
    for(int i=year;i<=year2;i++)//用年来循环
    {
        if((i%4==0&&i%100!=0)||i%400==0)//判断是否为闰年
        {
            montht[2]=29;
        }
        else montht[2]=28;
        if((i%10)*10+i/10%10<=12)//将年的后两位倒过来,并判断是否大于12。
        {
            if((i/100%10)*10+i/1000<=montht[(i%10)*10+i/10%10])//判断前两位倒回来是不是小于该月份的日期
            {
                ans++;//答案加1
            }
        }
    }
    cout<<ans<<endl;//输出结果
    return 0;
}

求过!!

猜你喜欢

转载自www.cnblogs.com/hulean/p/10799253.html
今日推荐