蓝桥杯(日期问题纯暴力)

纯纯暴力,写的想吐,玛德服了。

但是复习了vector去重方法,日期的合法性判断。

cpp 复制代码
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;
vector<int> res;
string s;
int d[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 

int leap(int year)
{
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1;
    else return 0;
}

int check(int year, int month, int data)
{
    if(year < 1960 || year > 2059) return -1;
    if(month < 1 || month > 12) return -1;
    if(data <= 0) return -1;
    if(month == 2){
        if(data > leap(year) + d[month]) return -1;
    }else{
        if(data > d[month]) return -1;
    }
    int res = (year * 100 + month ) * 100 + data;
    return res;
}

int main()
{
    cin >> s;
    int a = (s[0] - '0') * 10 + s[1] - '0';
    int b = (s[3] - '0') * 10 + s[4] - '0';
    int c = (s[6] - '0') * 10 + s[7] - '0';
    if(check(1900 + a, b, c) != -1) res.push_back(check(1900 + a, b, c));
    if(check(2000 + a, b, c) != -1) res.push_back(check(2000 + a, b, c));
    
    if(check(1900 + c, a, b) != -1) res.push_back(check(1900 + c, a, b));
    if(check(2000 + c, a, b) != -1) res.push_back(check(2000 + c, a, b));
    
    if(check(1900 + c, b, a) != -1) res.push_back(check(1900 + c, b, a));
    if(check(2000 + c, b, a) != -1) res.push_back(check(2000 + c, b, a));
    
    sort(res.begin(), res.end());
    res.erase(unique(res.begin(), res.end()), res.end());
    for(int i = 0; i < res.size(); i ++){
        int year = res[i] / 10000;
        int month = res[i] % 10000 / 100;
        int data = res[i] % 100;
        cout << year << "-";
        if(month >= 0 && month <= 9) cout << 0 << month << "-";
        else cout << month << "-";
        if(data >= 0 && data <= 9) cout << 0 << data << endl;
        else cout << data << endl;
    }
    return 0;
}
相关推荐
苏打豆12 分钟前
CRISP源码阅读——基于ROS2力矩反馈控制器与速度加速度零空间
c++·线性代数·算法·矩阵·机器人
蛋蛋的就会顺顺的40 分钟前
hot100——矩阵
java·数据结构·算法·leetcode·力扣
老歌老听老掉牙1 小时前
麻花钻切屑形态演变的力学机制与临界条件分析
python·算法·钻头
zhangfeng11331 小时前
国产GPU/AI算力芯片现状(修订完整版 · 截至2026年9月
人工智能·算法·ai编程·npu
学代码的CJY2 小时前
从代码出发理解时间复杂度与空间复杂度
数据结构·算法
白帽攻防录2 小时前
SRC 挖洞:WSO2 JWT 算法混淆绕过深度复盘,CVE-2026-5430 不支持的算法怎么变成管理员
网络·算法·安全·网络安全·jwt
小O的算法实验室3 小时前
IEEE TEVC,基于精确与DQN群智能调度方法用于多目标异构USV
算法
可乐ea3 小时前
AI Agent 工具调用准确性评测:选择错误与参数错误分开测
大数据·人工智能·算法·大模型·工具调用·ai智能体·agent评测
上位机妹子4 小时前
C 语言 数组删除指定元素(快慢指针法)
c语言·数据结构·算法
心中有你02144 小时前
【路径规划】A*寻路算法最通俗易懂讲解(C语言完整实现+详细注释)
c语言·开发语言·算法