日期统计(蓝桥杯)

该篇文章讲述了如何利用编程技巧解决一个关于查找2023年合法日期子序列的问题,涉及暴力枚举、哈希表去重和优化算法。最终答案是235个日期。

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

 题目链接

P2077 - [蓝桥杯2023初赛] 日期统计 - New Online Judge

题目描述

小蓝现在有一个长度为 100 的数组,数组中的每个元素的值都在 0 到 9 的范围之内。
数组中的元素从左至右如下所示:

5 6 8 6 9 1 6 1 2 4 9 1 9 8 2 3 6 4 7 7 5 9 5 0 3 8 7 5 8 1 5 8 6 1 8 3 0 3 7 9 2 7 0 5 8 8 5 7 0 9 9 1 9 4 4 6 8 6 3 3 8 5 1 6 3 4 6 7 0 7 8 2 7 6 8 9 5 6 5 6 1 4 0 1 0 0 9 4 8 0 9 1 2 8 5 0 2 5 3 3

现在他想要从这个数组中寻找一些满足以下条件的子序列:
1. 子序列的长度为 8;
2. 这个子序列可以按照下标顺序组成一个 yyyymmdd 格式的日期,并且
要求这个日期是 2023 年中的某一天的日期,例如 20230902,20231223。
yyyy 表示年份,mm 表示月份,dd 表示天数,当月份或者天数的长度只有一位时需要一个前导零补充。
请你帮小蓝计算下按上述条件一共能找到多少个不同的 2023 年的日期。
对于相同的日期你只需要统计一次即可。 
本题的结果为一个整数,在提交答案时只输出这个整数,输出多余的内容将无法得分。

方法一

数据量不大,多重for循环暴力枚举+哈希表查重,一个字符一个字符匹配,再判断是否是合法日期

代码

#include <iostream>
#include <unordered_map>
using namespace std;

const int arr[100] = {5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7, 5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5,
                      8, 6, 1, 8, 3, 0, 3, 7, 9, 2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3, 8, 5,
                      1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6, 1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1, 2,
                      8, 5, 0, 2, 5, 3, 3};
const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// 暴力枚举 + 哈希
int main()
{
    unordered_map<int, int> hash;
    int ans = 0;
    for (int a = 0; a < 100; ++a)
    {
        if (arr[a] == 2)
        {
            for (int b = a + 1; b < 100; ++b)
            {
                if (arr[b] == 0)
                {
                    for (int c = b + 1; c < 100; ++c)
                    {
                        if (arr[c] == 2)
                        {
                            for (int d = c + 1; d < 100; ++d)
                            {
                                if (arr[d] == 3)
                                {
                                    for (int e = d + 1; e < 100; ++e)
                                    {
                                        for (int f = e + 1; f < 100; ++f)
                                        {
                                            int month = 10 * arr[e] + arr[f];
                                            if (month > 12 || month == 0) // 判断是否合法
                                                continue;
                                            for (int g = f + 1; g < 100; ++g)
                                            {
                                                for (int h = g + 1; h < 100; ++h)
                                                {
                                                    int day = arr[g] * 10 + arr[h];
                                                    if (day > 31 || day == 0) // 判断是否合法
                                                        continue;
                                                    int date = 100 * month + day;
                                                    if (!hash.count(date) && days[month] >= day) // 查重, 判断日期是否合法
                                                        ans++;
                                                    hash[date]++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

方法二

思路转自上山打老虎D大佬的博客,正难则反,查询每个2023年的日期可以由这些数字拼出来,效率高,还可省去去重

代码

#include <iostream>
using namespace std;

const int arr[100] = {5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7, 5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5,
                      8, 6, 1, 8, 3, 0, 3, 7, 9, 2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3, 8, 5,
                      1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6, 1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1, 2,
                      8, 5, 0, 2, 5, 3, 3};
const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

// 正难则反, 统计哪些天可以由这些数字拼出来, 还可省去去重
int main()
{
    int ans = 0;
    for (int i = 1; i <= 12; ++i)
    {
        for (int j = 1; j <= days[i]; ++j)
        {
            // 枚举每个日期
            string s = "2023";
            if (i < 10)
                s += "0";
            s += to_string(i);
            if (j < 10)
                s += "0";
            s += to_string(j);
            int len = 0;
            // 判断是否能拼凑出当前日期
            for (int k = 0; k < 100; ++k)
            {
                if (arr[k] + '0' == s[len])
                    ++len;
                if (len == 8)
                {
                    ++ans;
                    break;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}

最后结果为235

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值