P2010回文日期

这道题是2016年普及组的题,难度等级为普及—。

这道题仍然是个模拟题。有两种策略:1.枚举回文,看日期是否存在2.枚举日期,看是否是回文。显然,前者要快很多,并且准确。本蒟蒻第一次便使用了后者,bug频出,看了题解后想到了前者。并且在取位与字符串处理上出了问题qaq。最后被卡了几个数据点......

1.字符串中转int:-48即可。
2.调bug的时候可以规范输出来查错误。
3.模拟题一定要思考好for谁快。
4.模拟题思路一定要清晰,别先动键盘,先动笔,确定好有把握的策略。

伪代码:
string str1,str2;
int year1,year2;
int month1,month2;
int day1,day2;
int ans=0;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int rday[12]={31,29,31,30,31,30,31,31,30,31,30,31};
bool judge_r(int x){
if(x%4000||(x%40&&x%10!=0)){
return true;
}
else return false;
}
int main(){
cin>>str1;
cin>>str2;
year1=(str1[0]-48)1000+(str1[1]-48)100+(str1[2]-48)10+(str1[3]-48);
year2=(str2[0]-48)
1000+(str2[1]-48)100+(str2[2]-48)10+(str2[3]-48);
month1=(str1[4]-48)10+(str1[5]-48);
month2=(str2[4]-48)
10+(str2[5]-48);
day1=(str1[6]-48)10+(str1[7]-48);
day2=(str2[6]-48)
10+(str2[7]-48);
/cout<<year1<<month1<<day1<<endl;
cout<<year2<<month2<<day2<<endl;
/
for(int i=year1;i<=year2;i++){
int month=(i%10)10+(i%100)/10; //回文
int date=i/1000+((i%1000)/100)
10;
// cout<<month<<" "<<date<<endl;
if(judge_r){//闰年不存在
if(month>12) continue;
if(date>rday[month]) continue;
}
else{//普通年不存在
if(month>12) continue;
if(date>day[month]) continue;
}
if(iyear1){//必须大于左端点
if(month<month1){
continue;
}
else{
if(month
month1&&date<day1){
continue;
}
}
}
if(iyear2){//必须小于右端点
if(month>month2){
continue;
}
else{
if(month
month2&&date>day2){
continue;
}
}
}
ans++;
}
cout<<ans;
return 0;
}

posted @ 2019-07-16 20:59  毛炯人  阅读(252)  评论(0编辑  收藏  举报