C++ 代码
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool month_day(int b,int c){
if(b==1) return c<=31;
else if(b==2) return c<=28;
else if(b==3) return c<=31;
else if(b==4) return c<=30;
else if(b==5) return c<=31;
else if(b==6) return c<=30;
else if(b==7) return c<=31;
else if(b==8) return c<=31;
else if(b==9) return c<=30;
else if(b==10) return c<=31;
else if(b==11) return c<=30;
else if(b==12) return c<=31;
return false;
}
bool lunar_month_day(int b,int c){
if(b==1) return c<=31;
else if(b==2) return c<=29;
else if(b==3) return c<=31;
else if(b==4) return c<=30;
else if(b==5) return c<=31;
else if(b==6) return c<=30;
else if(b==7) return c<=31;
else if(b==8) return c<=31;
else if(b==9) return c<=30;
else if(b==10) return c<=31;
else if(b==11) return c<=30;
else if(b==12) return c<=31;
return false;
}
bool check(int a,int b,int c){
//basic check
if(a>=1960&&a<=2059){
if(b>=1&&b<=12){
if(c>=1&&c<=31){
//speculate condition
//leap year
if(a%100!=0&&a%4==0||a%400==0){
if(lunar_month_day(b,c)){
return true;//satisfied
}
}
//normal year
else{
if(month_day(b,c)){
return true;
}
}
}
}
}
return false;
}
int main(){
string input;
vector<int>ans{};
int a=0,b=0,c=0;
int year1=0,year2=0,day=0,month=0,result1=0,result2=0;
//constrants:1.leap year 2.year,month,day limitation
cin>>input;
a=(input[0]-'0')*10+input[1]-'0';
b=(input[3]-'0')*10+input[4]-'0';
c=(input[6]-'0')*10+input[7]-'0';
//year/month/day
year1=2000+a;
year2=1900+a;
month=b;
day=c;
result1=year1*10000+month*100+day;
bool sss=check(year1,month,day);
if(check(year1,month,day)) ans.push_back(result1);
result2=year2*10000+month*100+day;
if(check(year2,month,day)) ans.push_back(result2);
//year/day/month
// year1=2000+a;
// year2=1900+a;
// month=c;
// day=b;
// result1=year1*10000+month*100+day;
// if(check(year1,month,day)) ans.push_back(result1);
// result2=year2*10000+month*100+day;
// if(check(year2,month,day)) ans.push_back(result2);
//month/year/day
// year1=2000+b;
// year2=1900+b;
// month=a;
// day=c;
// result1=year1*10000+month*100+day;
// if(check(year1,month,day)) ans.push_back(result1);
// result2=year2*10000+month*100+day;
// if(check(year2,month,day)) ans.push_back(result2);
//month/day/year
year1=2000+c;
year2=1900+c;
month=a;
day=b;
result1=year1*10000+month*100+day;
if(check(year1,month,day)) ans.push_back(result1);
result2=year2*10000+month*100+day;
if(check(year2,month,day)) ans.push_back(result2);
//day/year/month
// year1=2000+b;
// year2=1900+b;
// month=c;
// day=a;
// result1=year1*10000+month*100+day;
// if(check(year1,month,day)) ans.push_back(result1);
// result2=year2*10000+month*100+day;
// if(check(year2,month,day)) ans.push_back(result2);
//day/month/year
year1=2000+c;
year2=1900+c;
month=b;
day=a;
result1=year1*10000+month*100+day;
if(check(year1,month,day)) ans.push_back(result1);
result2=year2*10000+month*100+day;
if(check(year2,month,day)) ans.push_back(result2);
//sort
sort(ans.begin(),ans.end());
//delete the same number
vector<int>fi_ans{};
fi_ans.push_back(ans[0]);
for(int j=0,i=0;i<ans.size();i++){
if(ans[i]!=fi_ans[j]){
fi_ans.push_back(ans[i]);
j++;
}
}
for(int i=0;i<fi_ans.size();i++){
int a=fi_ans[i]/10000;
int b=(fi_ans[i]%10000)/100;
int c=fi_ans[i]%100;
if(b<10&&c<10){
cout<<a<<'-'<<'0'<<b<<'-'<<'0'<<c<<endl;
}
else if(b<10){
cout<<a<<'-'<<'0'<<b<<'-'<<c<<endl;
}
else if(c<10){
cout<<a<<'-'<<b<<'-'<<'0'<<c<<endl;
}
else{
cout<<a<<'-'<<b<<'-'<<c<<endl;
}
}
system("pause");
return 0;
}