该题貌似没有想到更好的解决方法,我本来想在将数据转化成string处理,不过也是知难而退了(格式输出),该题的难点除了格式输出以外还有顺序输出,使用暴力枚举能很好的解决这两个问题,我们从19600101枚举到20591231(还是纯数字最方便格式输出),即按时间顺序从小到大枚举,如果有满足条件的我们就可以输出,最后输出完3个日期自然就是顺序输出了
#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool Isleap(int year)//判断是不是闰年
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
return true;
}
else
{
return false;
}
}
bool check(int year,int month,int day)
{
if(Isleap(year))
{
m[2]=29;
}
else
{
m[2]=28;//注意这里如果不是闰年也要给它变回来,不然后面一直是闰年(坑)
}
if(month>=1&&month<=12&&day>=1&&day<=m[month])
{
return true;
}
else
return false;
}
int main()
{
int a,b,c,sign=0;
scanf("%d/%d/%d",&a,&b,&c);
for(int i=19600101;i<=20591231;i++)
{
int year=i/10000,month=i/100%100,day=i%100;
if(check(year,month,day))//检查格式
{
if((year%100==a&&month==b&&day==c)||(year%100==c&&month==a&&day==b)||(year%100==c&&month==b&&day==a))
{
sign++;
printf("%d-%02d-%02d\n",year,month,day);
}
}
if(sign==3)//输出了全部之后break
{
break;
}
}
return 0;
}