题目大意:就是有六种日期格式,给你一个字符串,判断它能组成多少种可能的日期。
第一次WA是:1.没有判重,2.没有特判题目要求的数据,3.判断天数时少了一个c(天数)>0的条件
第二次WA时:改正了错误2
第三次WA时:改正了错误3
AC那次改正了错误1。
题真的不难,但我觉得题出的非常好。你必须要读懂题目的细节才能AC。我重新读题的时候还好已经注意了错误2.但是那个判重真的没有考虑到,我觉得这是我这道题最大的弱点,毕竟错误3还是能更够仔细查找出来的,但那这个逻辑漏洞就很危险了!要判重!!!要判重!!!要判重!!!
参考代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char s[];
int aa, bb, cc;
int cu[]; bool runnian(int n)
{
if (((n % == ) && (n % != )) || (n % == && n % == ))
return true;
return false;
} int tianshu(int a)
{
if (a == ) return ;
else if (a == || a == || a == || a == ) return ;
else return ;
} bool YMD(int a, int b, int c)
{
if (runnian(a)) {
if (b == ) {
if (c>&&c <= ) {
return true;
}
else {
return false;
}
}
else {
if (b> && b <= && b != && c> && c <= tianshu(b)) return true;
else return false;
}
}
else
{
if (b> && b <= ) {
if (c> && c <= tianshu(b)) return true;
return false;
}
else return false;
} return false;
} int shu(int a, int b, int c)
{
return a * + b * + c;
} int main()
{
int T;
scanf("%d", &T);
for (int cas = ; cas <= T; cas++)
{
scanf("%s", s);
int a, b, c;
a = (s[] - '') * + (s[] - '');
b = (s[] - '') * + (s[] - '');
c = (s[] - '') * + (s[] - ''); int ans = ;
int nu = ;
if (YMD(a + , b, c)) { cu[nu++] = shu(a, b, c), ans++; }
if (YMD(a + , c, b)) { cu[nu++] = shu(a, c, b), ans++; }
if (YMD(b + , a, c)) { cu[nu++] = shu(b, a, c), ans++; }
if (YMD(b + , c, a)) { cu[nu++] = shu(b, c, a), ans++; }
if (YMD(c + , b, a)) { cu[nu++] = shu(c, b, a), ans++; }
if (YMD(c + , a, b)) { cu[nu++] = shu(c, a, b), ans++; } sort(cu, cu + nu);
int tot = ;
int tmp = cu[];
for (int i = ; i<nu; i++) {
if (cu[i] != tmp) tot++;
tmp = cu[i];
} if (a == && b == && c == ) tot = ;
if (ans == ) tot = ;
printf("Case #%d: %d\n", cas, tot);
}
return ;
}
再贴一份学长写的优秀代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
typedef long long ll;
const int maxn = 3e3 + ;
const int INF = 1e3 + ;
using namespace std; int month[] = {,,,,,,,,,,,,};
struct P {
int Y, M, D;
P() {}
P(int y, int m, int d) : Y(y), M(m), D(d) {}
bool operator < (P p) const {
if(Y != p.Y) return Y < p.Y;
if(M != p.M) return M < p.M;
return D < p.D;
}
bool right() {
if(Y % == || (Y % == && Y % != )) month[] = ;
else month[] = ;
if(Y < || Y > ) return false;
if(M < || M > ) return false;
if(D < || D > month[M]) return false;
return true;
}
};
int n, T, kase = ;
int y, m, d; int main() {
scanf("%d", &T);
while(T--) {
scanf("%d-%d-%d", &y, &m, &d);
if(y == && m == && d == ) {
printf("Case #%d: %d\n", kase++, );
continue;
}
set<P> st;
if(P(+y, m, d).right()) st.insert(P(+y, m, d));
if(P(+y, d, m).right()) st.insert(P(+y, d, m));
if(P(+m, y, d).right()) st.insert(P(+m, y, d));
if(P(+m, d, y).right()) st.insert(P(+m, d, y));
if(P(+d, m, y).right()) st.insert(P(+d, m, y));
if(P(+d, y, m).right()) st.insert(P(+d, y, m));
printf("Case #%d: %d\n", kase++, st.size());
}
return ;
}