(UVALive 7711)A - Confusing Date Format(水题,注意读题!)
题意:输入三个数a-b-c;有六种可能的日期,求有多少种合理的日期;04-05-01 特判,直接输出
我的枚举代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
#define mem(a) memset(a,0,sizeof(a))
const int N=1e3+5;
int dp,ne,a;
LL n;
int rq= {{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int judge(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)
return 1;
return 0;
}
int main()
{
int t;
int a,b,c;
scanf("%d",&t);
for(int cas=1; cas<=t; cas++)
{
scanf("%d-%d-%d",&a,&b,&c);
printf("Case #%d: ",cas);
if(a==4&&b==5&&c==1)
{
printf("1\n");
continue;
}
int ans=0;
int y,m,d,tmp;
if(b>0&&b<=12)
{
y=1900+a;///a1
tmp=judge(y);
m=b,d=c;
if(rq>=d&&d>0)
ans++;
//printf("a1 %d\n",ans);
if(a!=c)
{
y=1900+c;///c1
//printf("%d\n",y);
tmp=judge(y);
m=b,d=a;
if(rq>=d&&d>0)
ans++;
}
//printf("c1 %d\n",ans);
}
if(c!=b)
{
if(c>0&&c<=12)
{
y=1900+a;
tmp=judge(y);
m=c,d=b;
if(rq>=d&&d>0)
ans++;
// printf("a2 %d\n",ans);
if(a!=b)
{
y=1900+b;
tmp=judge(y);
m=c,d=a;
if(rq>=d&&d>0)
ans++;
//printf("b1 %d \n",ans);
}
}
}
if(a!=b&&a!=c)
{
if(a>0&&a<=12)
{
y=1900+b;
tmp=judge(y);
m=a,d=c;
if(m>0&&m<13&&rq>=d&&d>0)
ans++;
//printf("b2%d\n",ans);
if(b!=c)
{
y=1900+c;
tmp=judge(y);
m=a,d=b;
if(m>0&&m<13&&rq>=d&&d>0)
ans++;
//printf(" c2%d \n",ans);
}
}
}
printf("%d\n",ans);
}
return 0;
}
学长的:
预处理月份的天数,用集合去存年月日,就不用判断那么多了
#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 + 10;
const int INF = 1e3 + 10;
using namespace std;
int month = {0,31,28,31,30,31,30,31,31,30,31,30,31};
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 % 400 == 0 || (Y % 4 == 0 && Y % 100 != 0)) month = 29;
else month = 28;
if(Y < 1900 || Y > 1999) return false;
if(M < 1 || M > 12) return false;
if(D < 1 || D > month) return false;
return true;
}
};
int n, T, kase = 1;
int y, m, d;
int main() {
scanf("%d", &T);
while(T--) {
scanf("%d-%d-%d", &y, &m, &d);
if(y == 4 && m == 5 && d == 1) {
printf("Case #%d: %d\n", kase++, 1);
continue;
}
set<P> st;
if(P(1900+y, m, d).right()) st.insert(P(1900+y, m, d));
if(P(1900+y, d, m).right()) st.insert(P(1900+y, d, m));
if(P(1900+m, y, d).right()) st.insert(P(1900+m, y, d));
if(P(1900+m, d, y).right()) st.insert(P(1900+m, d, y));
if(P(1900+d, m, y).right()) st.insert(P(1900+d, m, y));
if(P(1900+d, y, m).right()) st.insert(P(1900+d, y, m));
printf("Case #%d: %d\n", kase++, st.size());
}
return 0;
}
文档来源:51CTO技术博客https://blog.51cto.com/u_13696685/2989451
页:
[1]