题目
思路:
1.注意重复和排序问题,这是非常难解决的
2.自己:一共有三种情况,列举出来。判断是否符合日期要求,符合保存在结构体数组中。然后进行结构体字典排序。最后判断一下再重复输出。
3.y总:因为时间复杂度不超过一亿,所以直接判断从最小到最大的范围,可以完美避开重复和排序问题。
代码:
#include <cstring>
#include <iostream>
#include<cstdio>
#include <algorithm>
using namespace std;
int aa,bb,cc,a,b,c;
int v[20]={0,31,28,31,30,31,30,31,31,30,31,30,31};//让下标 0 位置为 0
struct ji
{
int a,b,c;
}p[10];
int cmp(ji x,ji y)//结构体排序专用 cmp
{
if(x.a>y.a)
{
return 1;
}
if(x.a==y.a)
{
if(x.b>y.b)
{
return 1;
}
if(x.b==y.b)
{
if(x.c>y.c) return 1;
else return 0;
}
else return 0;
}
else return 0;
}
int pan()
{
if(a>2059||a<1960) return 0;
if(b>12||b<1) return 0;
if(!c) return 0;
if(b!=2)//必须写两个 if 因为写一个会自动跳到 else 语句中 导致错误
{
if(c>v[b]) return 0;
}
else{
int z=28;//初始值为 28
if(a%4==0) z=29;
if(c>z) return 0;
}
return 1;
}
int main()
{
string s;
cin>>s;
aa=(s[0]-'0')*10+(s[1]-'0');
bb=(s[3]-'0')*10+(s[4]-'0');
cc=(s[6]-'0')*10+(s[7]-'0');
int x=0;
a=aa,b=bb,c=cc;
a=2000+a;
if(pan())
{
p[x].a=a;
p[x].b=b;
p[x++].c=c;
}
a=aa;//每次加 1900 时都需要重置 a
a=1900+a;
if(pan()) {
p[x].a=a;
p[x].b=b;
p[x++].c=c;
}
a=cc;b=aa;c=bb;
a=2000+a;
if(pan()) {
p[x].a=a;
p[x].b=b;
p[x++].c=c;
}
a=cc;
a=1900+a;
if(pan()){
p[x].a=a;
p[x].b=b;
p[x++].c=c;
}
a=cc;b=bb;c=aa;
a=2000+a;
if(pan()) {
p[x].a=a;
p[x].b=b;
p[x++].c=c;
}
a=cc;
a=1900+a;
if(pan()) {
p[x].a=a;
p[x].b=b;
p[x++].c=c;
}
for(int i=0;i<x;i++)
{
for(int j=i;j<x;j++)
{
if(cmp(p[i],p[j])==1)
swap(p[i],p[j]);//如果是结构体多个数字排序,必须自己写函数排序
}
}
for(int i=0;i<x;i++)
{
if(p[i].a==p[i+1].a&&p[i].b==p[i+1].b&&p[i].c==p[i+1].c) continue;
printf("%d-%02d-%02d\n",p[i].a,p[i].b,p[i].c);
}
return 0;
}
y总:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check_valid(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 % 100 && year % 4 == 0 || year % 400 == 0;
if (day > 28 + leap) return false;
}
return true;
}
int main()
{
int a, b, c;
scanf("%d/%d/%d", &a, &b, &c);//学习y总的输入,有规则的输入时,可以直接用 printf
for (int date = 19600101; date <= 20591231; date ++ )//时间复杂度不超过 1 亿
{
int year = date / 10000, month = date % 10000 / 100, day = date % 100;
if (check_valid(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);
}
}
return 0;
}