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

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

但是复习了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;
}
相关推荐
weixin_307779134 小时前
软件演示环境动态扩展与成本优化:基于目标跟踪与计划扩展的AWS Auto Scaling策略
算法·云原生·云计算·aws
Carl_奕然4 小时前
【机器视觉】一文掌握常见图像增强算法。
人工智能·opencv·算法·计算机视觉
放羊郎4 小时前
人工智能算法优化YOLO的目标检测能力
人工智能·算法·yolo·视觉slam·建图
无敌最俊朗@4 小时前
友元的作用与边界
算法
Miraitowa_cheems5 小时前
LeetCode算法日记 - Day 104: 通配符匹配
linux·数据结构·算法·leetcode·深度优先·动态规划
小龙报5 小时前
《DevC++支持C++11等与其软件分辨率低的解决办法》
c语言·c++·windows·蓝桥杯·pat考试·学习方法·dvc++
程序员东岸5 小时前
从零开始学二叉树(上):树的初识 —— 从文件系统到树的基本概念
数据结构·经验分享·笔记·学习·算法
甄心爱学习6 小时前
数据挖掘11-分类的高级方法
人工智能·算法·分类·数据挖掘
爪哇部落算法小助手7 小时前
每日两题day44
算法
不穿格子的程序员7 小时前
从零开始写算法——二分-搜索二维矩阵
线性代数·算法·leetcode·矩阵·二分查找