蓝桥杯:日期统计讲解(C++)

日期统计

本题来自于:2023年十四届省赛大学B组真题

主要考察:暴力。

代码放在下面,代码中重要的细节全都写了注释,非常清晰明了:

cpp 复制代码
#include <bits/stdc++.h>  //万能头文件
using namespace std;

int main() {
    int array[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
    };  //先按题目要求放到数组中

    int daysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    //2023年每月的天数,电脑上自带日期,可以随便查   ,0索引不用,从1索引开始用,代表1月份
    int ans = 0;  //记录符合条件的日期

    for (int month = 1; month <= 12; ++month) {  //2023年已经确定,从月份开始
        for (int day = 1; day <= daysInMonth[month]; ++day) {//每个月的天数
            int dateSeq[8] = {2, 0, 2, 3, month / 10, month % 10, day / 10, day % 10};//用数组保存长度为8的日期
            int k = 0;   
            for (int i = 0; i < 100; ++i) {//i < 100 ,代表要开始遍历数组了(arr数组)
                if (array[i] == dateSeq[k]) {
                    ++k;
                    if (k == 8) {
                        ans++;
                        break;
                    }
                }
            }
        }
    }
    cout << ans;  //把结果输出
    return 0;
}
相关推荐
淡海水4 分钟前
【原理】Struct 和 Class 辨析
开发语言·c++·c#·struct·class
西工程小巴37 分钟前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
Tina学编程1 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
Moonbit1 小时前
MoonBit Perals Vol.06: MoonBit 与 LLVM 共舞 (上):编译前端实现
后端·算法·编程语言
Nuyoah11klay1 小时前
华清远见25072班C语言学习day11
c语言·指针·回调函数
青草地溪水旁1 小时前
UML函数原型中stereotype的含义,有啥用?
c++·uml
青草地溪水旁2 小时前
UML函数原型中guard的含义,有啥用?
c++·uml
百度Geek说2 小时前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven2 小时前
轻松掌握数据结构:二叉树
后端·算法·面试
big_eleven2 小时前
轻松掌握数据结构:二叉查找树
后端·算法·面试