【蓝桥杯】日期问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。

小明知道这些日期都在1960年1月1日至2059年12月31日。

令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。

更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入格式
一个日期,格式是”AA/BB/CC”。

即每个’/’隔开的部分由两个 0-9 之间的数字(不一定相同)组成。

输出格式
输出若干个不相同的日期,每个日期一行,格式是”yyyy-MM-dd”。

多个日期按从早到晚排列。

数据范围

0≤A,B,C≤9

输入样例:

02/03/04

输出样例:

2002-03-04
2004-02-03
2004-03-02

1、首先读取到这个日期
2、把这个日期安排成题中给出的三种格式
3、挨个判断一下这个是不是一个合法的日期

因为要求他按照时间顺序输出,所以把他填充成一个数字之后进行排序,同时判断是否在题中给定的范围内的时候也是用数字大小来比较;

代码如下

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
vector<int> vdate;

int date1 = 19600101, date2 = 20591231;

void check(int y, int m, int d)
{
    int date = (y * 100 + m) * 100 + d;
    if(date < date1 || date > date2)    return;
    
    if(m < 0 || m > 12) return;
    if(d == 0 || (m != 2 && d > days[m])) return;
    
    if(m == 2)
    {
        int leap = y % 100 && y % 4 == 0 || y % 400 == 0;
        if(d > days[m] + leap)   return;
    }
    vdate.push_back(date);
}

void p(int date)
{
    int y = date / 10000;
    int m = (date / 100) % 100;
    int d = date % 100;
    
    cout << y << '-';
    
    if(m < 10)
        cout << '0' << m << '-';
    else
        cout << m << '-';
        
    if(d < 10)
        cout << '0' << d << endl;
    else
        cout << d << endl;
        
    return;
}

int main()
{
    int x = 19, y = 20;
    int a, b, c;
    scanf("%d/%d/%d", &a, &b, &c);
    
    check(x * 100 + a, b, c);
    check(y * 100 + a, b, c);
    
    check(x * 100 + c, a, b);
    check(y * 100 + c, a, b);
    
    check(x * 100 + c, b, a);
    check(y * 100 + c, b, a);
    
    sort(vdate.begin(), vdate.end());
    
    for(int i = 0; i < vdate.size(); i ++ )
        if(vdate[i] != vdate[i+1])
            p(vdate[i]);
    
    return 0;
}

这样写就是直接的判断,几乎没有循环,时间复杂度肯定没得问题;但是这代码看起来长度有点过分
提供另一种想法:枚举
1、按数字对这个范围内进行枚举
2、数字转换成日期、判断这个日期是否合法
3、判断这个日期是否符合题目的输入

代码如下

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool check(int year, int month, int day)
{
    if(month == 0 || month > 12)    return false;
    if(day == 0 || (month != 2 && day > days[month]) )  return false;
    
    if(month == 2)	//平年闰年的2月不同
    {
        int leap = year % 100 && year % 4 == 0 || year % 400 == 0;
        if(day > days[month] + leap) return false;
    }
    
    return true;
}


int main()
{
    int a, b, c;
    scanf("%d/%d/%d", &a, &b, &c);
    
    for(int i = 19600101; i <= 20591231; i ++ )
    {
        int year = i / 10000;
        int month = (i / 100) % 100;
        int day = i % 100;
        if(check(year, month, day))
        {
            if( (year % 100 == a && month == b && day == c) ||
            (month == a && day == b && year % 100 == c) ||
            (day == a && month == b && year % 100 == c) )
            
                printf("%d-%02d-%02d\n", year, month, day);
        }
    }
    
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值