代码如下:
直接判断,用了重写关系符,逻辑还是很简单
#include<iostream>
using namespace std;
#include<string>
struct date
{
int year,month,day;
bool operator<(date &d)
{
if(this->year<d.year)
{
return true;
}
else if(this->year==d.year&&this->month<d.month)
{
return true;
}
else if(this->year==d.year&&this->month==d.month&&this->day<d.day)
{
return true;
}
else return false;
}
bool operator=(date &d)
{
this->day=d.day;
this->year=d.year;
this->month=d.month;
}
} dates[3];
int transa(int a)
{
if(a>59)
{
a=1900+a;
}
else a=2000+a;
return a;
}
bool checkdates(int year,int month,int day)
{
year=transa(year);
int leap;
int monday[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
if(month<1||month>12)return false;
if(year%400==0||(year%4==0&&year%100!=0))leap=1;
else leap=0;
monday[1]+=leap;
if(day<1||day>monday[month-1])return false;
return true;
}
void innidates(int a,int b,int c,int i)
{
a=transa(a);
dates[i].year=a;
dates[i].month=b;
dates[i].day=c;
}
int main()
{
string str;
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
int num=0;
if(checkdates(a,b,c))
{
innidates(a,b,c,num);
num++;
// cout<<a<<" "<<b<<" "<<c<<endl;
}
if(checkdates(c,a,b))
{
innidates(c,a,b,num);
num++;
// cout<<c<<" "<<a<<" "<<b<<endl;
}
if(checkdates(c,b,a))
{
innidates(c,b,a,num);
num++;
//cout<<c<<" "<<b<<" "<<a<<endl;
}
//cout<<num<<endl;
for(int i=1; i<num; i++)
{
if(dates[i] < dates[i-1]) //若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
{
int j= i-1;
date x = dates[i]; //复制为哨兵,即存储待排序元素
dates[i]= dates[i-1]; //先后移一个元素
while(x < dates[j]) //查找在有序表的插入位置
{
dates[j+1] = dates[j];
j--; //元素后移
}
dates[j+1] = x; //插入到正确位置
}
}
for(int i=0; i<num; i++)
{
if(dates[i].year==dates[i+1].year&&dates[i].month==dates[i+1].month&&dates[i].day==dates[i+1].day)
{
continue;
}
if(dates[i].month>9&&dates[i].day>9)
{
printf("%d-%d-%d\n",dates[i].year,dates[i].month,dates[i].day);
}
else if(dates[i].month>9&&dates[i].day<10)
{
printf("%d-%d-0%d\n",dates[i].year,dates[i].month,dates[i].day);
}
else if(dates[i].month<10&&dates[i].day<10)
{
printf("%d-0%d-0%d\n",dates[i].year,dates[i].month,dates[i].day);
}
else if(dates[i].month<10&&dates[i].day>9)
{
printf("%d-0%d-%d\n",dates[i].year,dates[i].month,dates[i].day);
}
}
return 0;
}