(UVALive 7711)A - Confusing Date Format(水题,注意读题!)

本文介绍了一种通过枚举和预处理方法来确定由三个输入数字构成的所有可能合理日期数量的算法。该算法考虑了平年与闰年的差异,并对特定日期进行了特判处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:输入三个数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[N],ne[N],a[N];
LL n;
int rq[2][13]= {{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[tmp][m]>=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[tmp][m]>=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[tmp][m]>=d&&d>0)
                    ans++;
                // printf("a2 %d\n",ans);
                if(a!=b)
                {
                    y=1900+b;
                    tmp=judge(y);
                    m=c,d=a;
                    if(rq[tmp][m]>=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[tmp][m]>=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[tmp][m]>=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[15] = {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[2] = 29;
        else month[2] = 28;
        if(Y < 1900 || Y > 1999) return false;
        if(M < 1 || M > 12) return false;
        if(D < 1 || D > month[M]) 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值