P2010 回文日期 题解

这题其实就是纯暴力,暴力,再暴力,毫无技巧可言(总之您怎么乱搞都不会超时QAQ)

首先,根据题意,我们明白每年自多产生一个回文日期,因为对于每年的三百多天,前四位是固定的。

所以,我们只需要进行一个从date1到date2之间的循环枚举,通过前四位找到后四位,再判断这八位数是否回文即可。

特别注意:

①:(其实题目里有讲)

一个年份是闰年当且仅当它满足下列两种情况其中的一种:

1.这个年份是4的整数倍,但不是100的整数倍;

2.这个年份是400的整数倍;

②:看到有些神犇的代码里对于“92200229”这个日期进行了特判,其实本蒟蒻感觉不需要,因为这个2月29日和9220年的条件不矛盾;

③:闰年2月有29日。

别问我这恶心的代码是怎么打出来的…… 

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 int s[9],e[9],sum;
 6 bool run(int need)
 7 {
 8     if (need%4==0&&need%100!=0) return true;
 9     else if (need%400==0) return true;
10     else return false;
11 }
12 int main()
13 {
14     for (int i=1;i<=8;i++) scanf("%1d",&s[i]);
15     for (int i=1;i<=8;i++) scanf("%1d",&e[i]);
16     #define __const__   s[1]*1000+s[2]*100+s[3]*10+s[4]
17     #define ___const___ e[1]*1000+e[2]*100+e[3]*10+e[4]
18     for (int i= __const__;i<=___const___;i++)
19     {
20         int cmp[9]={0,i/1000,i%1000/100,i%100/10,i%10,i%10,i%100/10,i%1000/100,i/1000};
21         if (!(cmp[1]==cmp[8]&&cmp[2]==cmp[7]&&cmp[3]==cmp[6]&&cmp[4]==cmp[5])) continue;
22         #define _const_ cmp[5]*10+cmp[6]
23         #define CONST cmp[7]*10+cmp[8]
24         if (_const_>12||_const_<1) continue;
25         if (CONST>31||CONST<1) continue;
26         if ((_const_==1||_const_==3||_const_==5||_const_==7||_const_==8||_const_==10||_const_==12)&&CONST>31) continue;
27         if ((_const_==4||_const_==6||_const_==9||_const_==11)&&(CONST>30)) continue;
28         if ((_const_==2&&CONST>29&&run(cmp[1]*1000+cmp[2]*100+cmp[3]*10+cmp[4]))) continue;
29         if ((_const_==2&&CONST>28&&!run(cmp[1]*1000+cmp[2]*100+cmp[3]*10+cmp[4]))) continue;
30         if (i==___const___&&(_const_>e[5]*10+e[6]||(_const_==e[5]*10+e[6]&&CONST>e[7]*10+e[8]))) continue;
31         if (i==__const__&&(_const_<s[5]*10+s[6]||(_const_==s[5]*10+s[6]&&CONST<s[7]*10+s[8]))) continue;
32         sum++;
33     }
34     printf("%d\n",sum);
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/tangwuling/p/10029552.html