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

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

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

#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;
}

相关推荐

  1. 日期问题暴力

    2024-03-10 19:52:04       41 阅读
  2. Acwing2024日期问题

    2024-03-10 19:52:04       36 阅读
  3. C/C++日期问题

    2024-03-10 19:52:04       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 19:52:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 19:52:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 19:52:04       82 阅读
  4. Python语言-面向对象

    2024-03-10 19:52:04       91 阅读

热门阅读

  1. 1. Linux应用编程概念

    2024-03-10 19:52:04       40 阅读
  2. 环境搭建SpringSecurity

    2024-03-10 19:52:04       29 阅读
  3. 线程的使用

    2024-03-10 19:52:04       36 阅读
  4. 网络综合布线

    2024-03-10 19:52:04       42 阅读
  5. 01-Linux系统概述,安装网络和防火墙配置

    2024-03-10 19:52:04       45 阅读