通用的模版
判断闰年的模板
bool is_leap(int year)
{
return year%4==0&&year%100!=0||year%400==0;
}
月份天数的模板
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
取年月日的模板(8位输入)
int year=x/10000;
int month=x%10000/100;
int day=x%100;
双指针判断八位数的回文日期模板
//双指针写法:判断回文日期(八位)
bool check1(string s)
{
int len=s.size();
for(int i=0,j=len-1;i<j;i++,j--)
{
if(s[i]!=s[j])return false;
}
return true;
}
可以使用to_string(数字)函数将数字日期转变为string类型的日期,然后使用该模板
判断日期是否符合规范的模板(8位输入)
bool check(int date)
{
int year=date/10000;
int month=date%10000/100;
int day=date%100;
if(month==0||month>12)return false;
if(day==0||month!=2&&day>days[month])return false;
//单独判断二月
if(month==2)
{
int leap=(year%100&&year%4==0||year%400==0);
if(day>28+leap)return false;
}
return true;
}
基础的题目
#include<bits/stdc++.h>
using namespace std;
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//把每月的天数求出来
int func(int x)
{
int y=x/10000;
int m=x%10000/100;
int d=x%10000%100;
a[2]=(y%4==0&&y%100!=0||y%400==0?29:28);
while (m -- ){
d+=a[m];
}
while(y--)
{
d+=(y%4==0&&y%100!=0||y%400==0?366:365);
}
return d;
}
int main()
{
int a,b;
while(cin>>a>>b)cout<<abs(func(a)-func(b))+1<<endl;
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int date1,date2;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int date)
{
int year=date/10000;
int month=date%10000/100;
int day=date%100;
if(month==0||month>12)return false;
if(day==0||month!=2&&day>days[month])return false;
if(month==2)
{
int leap=(year%100&&year%4==0||year%400==0);
if(day>28+leap)return false;
}
return true;
}
int main()
{
int res=0;
cin>>date1>>date2;
for(int i=1000;i<=9999;i++)
{
int date=i,x=i;
for(int j=0;j<4;j++)
{
date=date*10+x%10;
x/=10;
}
//枚举得到的回文日期是否符合规范
if(date<=date2&&date>=date1&&check(date))res++;
}
cout<<res<<endl;
return 0;
}
枚举的思路
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int year,int month,int day)
{
if(month==0||month>12)return false;
if(day==0)return false;
if(month!=2){if(day>days[month])return false;}
else
{
int leap=(year%4==0&&year%100!=0||year%400==0);
if(day>days[month]+leap)return false;
}
return true;
}
int main()
{
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
//枚举的思路
for(int date=19600101;date<=20591231;date++)
{
int year = date / 10000, month = date % 10000 / 100, day = date % 100;
if(check(year,month,day))
{
if(year%100==a&&month==b&&day==c||
month==a&&day==b&&year%100==c||
day==a&&month==b&&year%100==c)
{
printf("%d-%02d-%02d\n", year, month, day); //补前导0
}
}
}
return 0;
}
判断回文日期(格式加强版)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int date)
{
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if(month==0||month>12)return false;
if(day==0)return false;
if(month!=2){if(day>days[month])return false;}
else
{
int leap=(year%4==0&&year%100!=0||year%400==0);
if(day>days[month]+leap)return false;
}
return true;
}
//双指针写法:判断回文日期(八位)
bool check1(string s)
{
int len=s.size();
for(int i=0,j=len-1;i<j;i++,j--)
{
if(s[i]!=s[j])return false;
}
return true;
}
//在回文日期的基础上特判
bool check2(string s) //判断是否是ABABBABA 型的回文日期
{
if(check1(s)) //首先该日期要满足回文格式
{
if(s[0]!=s[2] || s[1]!= s[3] || s[0] == s[1]) return false;
return true;
}
}
int main()
{
int date,flag=0;
cin>>date;
for(int i = date +1; ;i++)
{
if(check(i))
{
string s = to_string(i);//to_string(i)函数
if(check1(s)&&!flag) //输出最近的那个回文日期
{
cout<<i<<endl;
flag = 1; //标记一下,避免多次输出
}
if(check2(s)) //输出ABABBABA 型的回文日期
{
cout<<i<<endl;
return 0;
}
}
}
return 0;
}