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

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

但是复习了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;
}
相关推荐
BlockWay1 小时前
WEEX Labs 周度观察:微软-OpenAI 合作调整与AI 多云趋势
大数据·人工智能·算法·安全·microsoft
风筝在晴天搁浅1 小时前
快手 CodeTop LeetCode 224.基本计算器
数据结构·算法·leetcode
Smoothcloud润云1 小时前
5大功能精修,重构AI算力使用体验!
java·人工智能·windows·算法·重构·编辑器·sublime text
计算机安禾1 小时前
【算法分析与设计】第41篇:确定性与非确定性多项式时间:P与NP的形式化
算法
浮午1 小时前
字节AI Agent开发面试全解析:15道高频问题+深度答案
人工智能·面试·职场和发展
leo__5202 小时前
随机接入退避算法过程模拟实现
网络·算法
-To be number.wan2 小时前
算法日记 | STL- sort排序
c++·算法
玖釉-2 小时前
Vulkan 中 Shader 的 vert、frag、mesh、comp 全面解析:作用、关系、特点与工程实践
开发语言·c++·windows·算法·图形渲染
智者知已应修善业3 小时前
【51单片机2个外部中断切换LED花样】2024-1-3
c++·经验分享·笔记·算法·51单片机
8Qi83 小时前
LeetCode 31:下一个排列(Next Permutation)—— 完整题解笔记 ✅
笔记·算法·leetcode·指针·思维·排列