2016 Asia Jakarta Regional Contest A - Confusing Date Format UVALive 7711 【模拟题】

A - Confusing Date Format

题目大意:就是有六种日期格式,给你一个字符串,判断它能组成多少种可能的日期。

第一次WA是:1.没有判重,2.没有特判题目要求的数据,3.判断天数时少了一个c(天数)>0的条件

第二次WA时:改正了错误2
第三次WA时:改正了错误3
AC那次改正了错误1。
题真的不难,但我觉得题出的非常好。你必须要读懂题目的细节才能AC。我重新读题的时候还好已经注意了错误2.但是那个判重真的没有考虑到,我觉得这是我这道题最大的弱点,毕竟错误3还是能更够仔细查找出来的,但那这个逻辑漏洞就很危险了!要判重!!!要判重!!!要判重!!!
参考代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 char s[10];
 7 int aa, bb, cc;
 8 int cu[8];
 9 
10 bool runnian(int n)
11 {
12     if (((n % 4 == 0) && (n % 100 != 0)) || (n % 400 == 0 && n % 100 == 0))
13         return true;
14     return false;
15 }
16 
17 int tianshu(int a)
18 {
19     if (a == 2) return 28;
20     else if (a == 4 || a == 6 || a == 9 || a == 11) return 30;
21     else return 31;
22 }
23 
24 bool YMD(int a, int b, int c)
25 {
26     if (runnian(a)) {
27         if (b == 2) {
28             if (c>0&&c <= 29) {
29                 return true;
30             }
31             else {
32                 return false;
33             }
34         }
35         else {
36             if (b>0 && b <= 12 && b != 2 && c>0 && c <= tianshu(b)) return true;
37             else return false;
38         }
39     }
40     else
41     {
42         if (b>0 && b <= 12) {
43             if (c>0 && c <= tianshu(b)) return true;
44             return false;
45         }
46         else    return false;
47     }
48 
49     return false;
50 }
51 
52 int shu(int a, int b, int c)
53 {
54     return a * 10000 + b * 100 + c;
55 }
56 
57 int main()
58 {
59     int T;
60     scanf("%d", &T);
61     for (int cas = 1; cas <= T; cas++)
62     {
63         scanf("%s", s);
64         int a, b, c;
65         a = (s[0] - '0') * 10 + (s[1] - '0');
66         b = (s[3] - '0') * 10 + (s[4] - '0');
67         c = (s[6] - '0') * 10 + (s[7] - '0');
68 
69         int ans = 0;
70         int nu = 0;
71         if (YMD(a + 1900, b, c)) { cu[nu++] = shu(a, b, c), ans++; }
72         if (YMD(a + 1900, c, b)) { cu[nu++] = shu(a, c, b), ans++; }
73         if (YMD(b + 1900, a, c)) { cu[nu++] = shu(b, a, c), ans++; }
74         if (YMD(b + 1900, c, a)) { cu[nu++] = shu(b, c, a), ans++; }
75         if (YMD(c + 1900, b, a)) { cu[nu++] = shu(c, b, a), ans++; }
76         if (YMD(c + 1900, a, b)) { cu[nu++] = shu(c, a, b), ans++; }
77 
78         sort(cu, cu + nu);
79         int tot = 1;
80         int tmp = cu[0];
81         for (int i = 1; i<nu; i++) {
82             if (cu[i] != tmp) tot++;
83             tmp = cu[i];
84         }
85 
86         if (a == 4 && b == 5 && c == 1) tot = 1;
87         if (ans == 0) tot = 0;
88         printf("Case #%d: %d\n", cas, tot);
89     }
90     return 0;
91 }

再贴一份学长写的优秀代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<vector>
 5 #include<iostream>
 6 #include<queue>
 7 #include<map>
 8 #include<stack>
 9 #include<set>
10 #include<algorithm>
11 typedef long long ll;
12 const int maxn = 3e3 + 10;
13 const int INF = 1e3 + 10;
14 using namespace std;
15 
16 int month[15] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
17 struct P {
18     int Y, M, D;
19     P() {}
20     P(int y, int m, int d) : Y(y), M(m), D(d) {}
21     bool operator < (P p) const {
22         if(Y != p.Y) return Y < p.Y;
23         if(M != p.M) return M < p.M;
24         return D < p.D;
25     }
26     bool right() {
27         if(Y % 400 == 0 || (Y % 4 == 0 && Y % 100 != 0)) month[2] = 29;
28         else month[2] = 28;
29         if(Y < 1900 || Y > 1999) return false;
30         if(M < 1 || M > 12) return false;
31         if(D < 1 || D > month[M]) return false;
32         return true;
33     }
34 };
35 int n, T, kase = 1;
36 int y, m, d;
37 
38 int main() {
39     scanf("%d", &T);
40     while(T--) {
41         scanf("%d-%d-%d", &y, &m, &d);
42         if(y == 4 && m == 5 && d == 1) {
43             printf("Case #%d: %d\n", kase++, 1);
44             continue;
45         }
46         set<P> st;
47         if(P(1900+y, m, d).right()) st.insert(P(1900+y, m, d));
48         if(P(1900+y, d, m).right()) st.insert(P(1900+y, d, m));
49         if(P(1900+m, y, d).right()) st.insert(P(1900+m, y, d));
50         if(P(1900+m, d, y).right()) st.insert(P(1900+m, d, y));
51         if(P(1900+d, m, y).right()) st.insert(P(1900+d, m, y));
52         if(P(1900+d, y, m).right()) st.insert(P(1900+d, y, m));
53         printf("Case #%d: %d\n", kase++, st.size());
54     }
55     return 0;
56 }

 

 
posted @ 2017-08-01 15:22  ╰追憶似水年華ぃ╮  阅读(216)  评论(0编辑  收藏  举报