• 第八届蓝桥杯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

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int mDayNum[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     5 
     6 struct date
     7 {
     8     int year;
     9     int month;
    10     int day;
    11 
    12     date(int y,int m,int d)    //构造函数 
    13     {
    14         year=y;
    15         month=m;
    16         day=d;
    17     }
    18     
    19     bool operator<(date other)const    //因为set默认将元素从小到大排列,自定义类需要重载<运算符 
    20     {
    21         if(year==other.year)
    22         {
    23             if(month==other.month)
    24             {
    25                 return day<other.day;
    26             }
    27             return month<other.month;
    28         }
    29         return year<other.year;
    30     }
    31 
    32     bool vial()    //判断年月日是否符合 
    33     {
    34         if(year<1960||year>2059)
    35             return false;
    36         if(month<=0||month>12)
    37             return false;
    38         if(year%4==0&&year%100!=0||year%400==0)
    39         {
    40             if(month==2)
    41             {
    42                 return day>=1&&day<=29;
    43             }
    44             return day>=1&&day<=mDayNum[month];
    45         }
    46         else
    47         {
    48             return day>=1&&day<=mDayNum[month];
    49         }
    50     }
    51 
    52     void print() const    //打印 
    53     {
    54         printf("%d-%02d-%02d
    ",year,month,day);
    55     }
    56 
    57 };
    58 
    59 set<date> ss;
    60 
    61 void insert(int a,int b,int c)    //加入集合前先判断 
    62 {
    63     date obj(a,b,c);
    64     if(obj.vial())
    65     {
    66         ss.insert(obj);
    67     }
    68 }
    69 
    70 int main()
    71 {
    72 
    73     freopen("input.txt","r",stdin);
    74     int a,b,c;
    75     scanf("%d/%d/%d",&a,&b,&c);        //输入:年/月/日 
    76     //共六种情况,分别进行判断即可 
    77     
    78     //年月日
    79     insert(1900+a,b,c);
    80     insert(2000+a,b,c);
    81 
    82     //月日年
    83     insert(1900+c,a,b);
    84     insert(2000+c,a,b);
    85 
    86     //日月年
    87     insert(1900+c,b,a);
    88     insert(2000+c,b,a);
    89 
    90     for(set<date>::iterator it=ss.begin();it!=ss.end();it++)
    91     {
    92         it->print();
    93     }
    94 
    95     return 0;
    96 }
  • 相关阅读:
    linux文件操作.md
    MinIO
    快速幂和快速乘
    LCP 03.机器人大冒险
    java 实现 js 方法调用
    UNITY STANDARD ASSETS源码学习笔记(二)Sun Shafts
    下雨Shader,触摸擦除雨点,声控闪电
    Unity Standard Assets源码学习笔记(一)SSAO
    【转发】vueSSR剖析
    locust中的监听器
  • 原文地址:https://www.cnblogs.com/WindSun/p/10554528.html
Copyright © 2020-2023  润新知