日期问题
一个函数判断日期是否合法,一个函数用来输出。思路比较简单,枚举即可
注意还要按时间顺序输出,并不是每一个都对应三个输出!
C++ 代码
#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||month!=2&&day>days[month])return false;
if(month==2){
int leap= year%400==0 || year%4==0&&year%100!=0;
if(day>28+leap)return false;
}
return true;
}
void print(int year,int month,int day){
if(year>=60)cout<<"19"<<year<<"-";
else if(year>=10) cout<<"20"<<year<<"-";
else cout<<"200"<<year<<"-";
if(month<10)cout<<"0"<<month<<"-";
else cout<<month<<"-";
if(day<10)cout<<"0"<<day<<"\n";
else cout<<day<<"\n";
return;
}
int main()
{
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
if(a==b&&a==c){//02/02/02只输出一个
print(a,b,c);return 0;
}
int ta=0,tb=0,tc=0;
if(a>c)ta=a,tb=b,tc=c,swap(a,c);
if(check(a,b,c))print(a,b,c);
if(ta){//12/12/06只输出两个
if(check(ta,tb,tc))print(ta,tb,tc);
return 0;
}
if(a>b)swap(a,b);
if(check(c,a,b))print(c,a,b);
if(check(c,b,a))print(c,b,a);
return 0;
}
根据输入的三个数字枚举了可能的三个日期,有特殊情况容易忽略