题解 | #[NOIP2016]回文日期# 运用库函数
[NOIP2016]回文日期
https://ac.nowcoder.com/acm/problem/16438
思路: 通过判断当前年份逆序后是否依旧合法即可
ps:好像数据有点问题,没有考虑月份和天数的起始问题也ac掉这题目了
#include<bits/stdc++.h>
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<int,int> PII;
string s,e;
int ans;
bool legal(string s,int year)
{
bool flag=false;
if((year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0))flag=true;//判断闰年
int month=stoi(s.substr(0,2));
int day=stoi(s.substr(2));
if(month < 1 || month > 12)return false;
//特判闰年2月份情况
if(month == 2 )
{
if(flag)
{
if(day < 1 || day > 29)return false;
}
else
{
if(day < 1 || day > 28)return false;
}
}
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if(day < 1 || day > 31)return false;
}
else if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day < 1 || day > 30)return false;
}
return true;
}
signed main()
{
io;
cin>>s>>e;
int sy=stoi(s.substr(0,4)),sm=stoi(s.substr(4,2)),sd=stoi(s.substr(6));
int ey=stoi(e.substr(0,4)),em=stoi(e.substr(4,2)),ed=stoi(e.substr(6));
for(int i=sy;i<=ey;i++)
{
string x=to_string(i);//当前年份
reverse(x.begin(),x.end());
if(legal(x,i))ans++;//逆序后判断是否为合法日期
}
cout<<ans<<endl;
return 0;
}