题目:1229. 日期问题
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int a[5],ct=0;
int mon[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x){
if(x<19600101||x>20591231) return false;
int m=x%10000/100;
int d=x%100;
int y=x/10000;
if(m<=0||m>=13||d<=0) return false;
if(m==2){
if((y%4==0)&&(y%100!=0)||y%400==0){
if(mon[2]+1<d) return false;
}else{
if(mon[2]<d) return false;
}
}else {
if(mon[m]<d) return false;
}
return true;
}
void get(int x,int y,int z){
if(x<=59) x=2000+x;
else x=1900+x;
int t=x*10000+y/10*1000+y%10*100+z/10*10+z%10;
if(check(t)){
a[ct++]=t;
}
}
int main(){
int x,y,z;
scanf("%d/%d/%d",&x,&y,&z);
get(x,y,z);
get(z,y,x);
get(z,x,y);
sort(a,a+ct);
for(int i=0;i<ct;i++){
int t=a[i];
printf("%d-%02d-%02d\n",t/10000,t%10000/100,t%100);
}
return 0;
}
下面的代码,我是想使用一下unique()和erase()结合去重
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
vector<int >a;
int mon[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x){
if(x<19600101||x>20591231) return false;
int m=x%10000/100;
int d=x%100;
int y=x/10000;
if(m<=0||m>=13||d<=0) return false;
if(m==2){
if((y%4==0)&&(y%100!=0)||y%400==0){
if(mon[2]+1<d) return false;
}else{
if(mon[2]<d) return false;
}
}else {
if(mon[m]<d) return false;
}
return true;
}
void get(int x,int y,int z){
if(x<=59) x=2000+x;
else x=1900+x;
int t=x*10000+y/10*1000+y%10*100+z/10*10+z%10;
if(check(t)){
a.push_back(t);
}
}
int main(){
int x,y,z;
scanf("%d/%d/%d",&x,&y,&z);
get(x,y,z);
get(z,y,x);
get(z,x,y);
sort(a.begin(),a.end());
// int b[5],b_ct=0;
// b[0]=a[0];
// for(int i=1;i<ct;i++){
// if(a[i]!=a[i-1]) b[++b_ct]=a[i];
// }
a.erase(unique(a.begin(),a.end()),a.end());
for(int i=0;i<a.size();i++){
int t=a[i];
printf("%d-%02d-%02d\n",t/10000,t%10000/100,t%100);
}
return 0;
}