考察:模拟
思路:
总共就三种方式,每个检验一下就行.但是注意日期不为0,且注意存在相同的答案,经过排序会将相同的日期排在一起.所以直接判断前一个是否与当前相同即可.
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int N =5; 6 int month[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 7 int a,b,c,s[N],cnt; 8 bool isleap(int y) 9 { 10 if(y%400==0) return 1; 11 if(y%4==0&&y%100!=0) return 1; 12 return 0; 13 } 14 bool check(int y,int m,int d) 15 { 16 month[2] = 28; 17 if(m>12||m<1) return 0; 18 if(y<=59&&y>=0) y = y+2000; 19 else y = y+1900; 20 if(isleap(y)) month[2] =29; 21 if(d<=0||d>month[m]) return 0; 22 return 1; 23 } 24 int get(int y,int m,int d) 25 { 26 if(y<=59&&y>=0) y = y+2000; 27 else y = y+1900; 28 return y*10000+m*100+d; 29 } 30 int main() 31 { 32 scanf("%d/%d/%d",&a,&b,&c); 33 if(check(a,b,c)) s[++cnt] = get(a,b,c); 34 if(check(c,a,b)) s[++cnt] = get(c,a,b); 35 if(check(c,b,a)) s[++cnt] = get(c,b,a); 36 sort(s+1,s+cnt+1); 37 for(int i=1;i<=cnt;i++) 38 { 39 if(s[i]==s[i-1]) continue; 40 int y = s[i]/10000,m = s[i]%10000/100,d = s[i]%100; 41 printf("%d-%02d-%02d\n",y,m,d); 42 } 43 return 0; 44 }