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

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

但是复习了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;
}
相关推荐
再卷也是菜35 分钟前
算法基础篇(8)贪心算法
算法·贪心算法·1024程序员节
AI科技星1 小时前
接近光速运动下的光速不变性:基于张祥前统一场论的推导与验证
数据结构·人工智能·经验分享·算法·计算机视觉
陈苏同学2 小时前
笔记1.4:机器人学的语言——三维空间位姿描述 (旋转矩阵 - 齐次变换矩阵 - 欧拉角 - 四元数高效表示旋转)
笔记·线性代数·算法·机器人
scx201310042 小时前
20251025 分治总结
数据结构·c++·算法
碧海银沙音频科技研究院2 小时前
DiVE长尾识别的虚拟实例蒸馏方法
arm开发·人工智能·深度学习·算法·音视频
居7然4 小时前
DeepSeek OCR:重新定义AI文档处理的“降本增效”新范式
人工智能·算法·语言模型·自然语言处理·大模型·ocr
while(1){yan}4 小时前
数据结构之堆
数据结构·python·算法
SleepyWhite0014 小时前
代码随想录Day61|Floyd 算法精讲、A * 算法精讲
算法·floyd算法·astar算法
Miraitowa_cheems4 小时前
LeetCode算法日记 - Day 84: 乘积为正数的最长子数组长度
数据结构·算法·leetcode·贪心算法·线性回归·深度优先·动态规划