AcWing 466. 回文日期
原题链接
简单
作者:
HiCode_001
,
2019-11-01 11:32:36
,
所有人可见
,
阅读 776
题意分析:
计算规定期间内的年月日是否都为会问数
思路分析:
1)枚举,因为为一个八位数,所有我们枚举左半边就可以
2)枚举左半边也就是年份,所以只需要从1000-9999就可以
3)判断是否在区间内
4)判断是否符合年月日规范
细节小技巧:
1)枚举左半边,分出每一位,还原8位数
2)判断年月日是否合法,闰年的判断,月儿分的判断,需要注意
C++ 代码
#include <iostream>
using namespace std;
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
bool check(int r){
int date = r%100;
int month = r%10000/100;
int year = r/10000;
if(!month||month>13||!date) return false;
if(month !=2 && date>months[month]) return false;
if(month==2){
bool leap = (year%4==0&&year%100!=0)||(year%400==0);
if(date>28+leap) return false;
}
}
int main(){
int date1,date2;
cin >> date1 >> date2;
int res = 0;
for(int i = 1000; i<=9999; i++){
int x = i ,r = i;
for(int j=1; j<=4;j++){
r = r*10+x%10;
x /=10;
if(r>=date1&&r<=date2&&check(r)) res++;
}
}
cout << res <<endl;
return 0;
}