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

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

但是复习了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;
}
相关推荐
C++ 老炮儿的技术栈41 分钟前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
yychen_java1 小时前
R-tree详解
java·算法·r-tree
MarkHard1232 小时前
Leetcode (力扣)做题记录 hot100(62,64,287,108)
算法·leetcode·职场和发展
一只鱼^_2 小时前
牛客练习赛138(首篇万字题解???)
数据结构·c++·算法·贪心算法·动态规划·广度优先·图搜索算法
一只码代码的章鱼3 小时前
Spring的 @Validate注解详细分析
前端·spring boot·算法
邹诗钰-电子信息工程3 小时前
嵌入式自学第二十一天(5.14)
java·开发语言·算法
↣life♚3 小时前
从SAM看交互式分割与可提示分割的区别与联系:Interactive Segmentation & Promptable Segmentation
人工智能·深度学习·算法·sam·分割·交互式分割
zqh176736464693 小时前
2025年阿里云ACP人工智能高级工程师认证模拟试题(附答案解析)
人工智能·算法·阿里云·人工智能工程师·阿里云acp·阿里云认证·acp人工智能
fie88894 小时前
用模型预测控制算法实现对电机位置控制仿真
算法
Kent_J_Truman4 小时前
【交互 / 差分约束】
算法