第八届蓝桥杯省赛C++B组 日期问题

标题:日期问题

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。 

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

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

输入
----
一个日期,格式是"AA/BB/CC"。  (0 <= A, B, C <= 9) 

输出
----
输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。 

样例输入
----
02/03/04 

样例输出
----
2002-03-04 
2004-02-03 
2004-03-02 

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

注意:
main函数需要返回0;
只使用ANSI C/ANSI C++ 标准;
不要调用依赖于编译环境或操作系统的特殊函数。
所有依赖的函数必须明确地在源文件中 #include
不能通过工程设置而省略常用头文件。

提交程序时,注意选择所期望的语言类型和编译器类型。

 

思路:感觉有很多细节,比赛的时候也是敲了好长,现在已经完全不记得了……这里就贴一个别人的思路和代码吧(http://blog.csdn.net/y1196645376/article/details/69718192/),把三种日期格式对应的日期都枚举出来,然后排除非法日期和不在题目所述范围内的日期,最后去重排序就可以了。

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int md[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
struct date
{
    int year;
    int month;
    int day;

    date(int y,int m,int d)
    {
        year = y;
        month = m;
        day = d;
    }

    bool operator < (date other)const{
        if(year == other.year)
        {
            if(month == other.month)
                return day 2059) return false;
        if(month <= 0 || month > 12) return false;
        if(year % 400 == 0 || year % 100 != 0 && year % 4 == 0){
            //闰年
            if(month == 2){
                return day >= 1 && day <= 29;
            } 
            return day >= 1 && day <= md[month]; 
        }else{
            return day >= 1 && day <= md[month];
        }
    }
    void print()const{
        printf("%d-%02d-%02d\n",year,month,day);
    }
};
set ss;  //利用set容器来去重排序

void insert(int a,int b,int c)
{
    date obj(a,b,c);
    if(obj.vial()) ss.insert(obj);
}
int main()
{
    int a,b,c;
    scanf("%d/%d/%d",&a,&b,&c);
    //年月日 
    insert(1900+a,b,c);
    insert(2000+a,b,c);
    //月日年
    insert(1900+c,a,b);
    insert(2000+c,a,b);
    //日月年 
    insert(1900+c,b,a);
    insert(2000+c,b,a);

    set::iterator it = ss.begin();
    for(; it != ss.end() ; it ++)
    {
        it->print();
    }
    return 0;
}

 

你可能感兴趣的:(蓝桥杯)