• 第八届蓝桥杯省赛第七题---日期问题



    标题:日期问题

    小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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 <xxx>

    分析:

    1. 设置一个日期类(感觉日期问题这样写不错),编写判断日期格式函数,自定义排序

    2. 输入可以用 scanf("%d/%d/%d") 直接输入整型数

    3. insert(年, 月, 日) , insert(日,月,年), insert(日,年,月), 可以插入到set中,自动进行删选重复和进行排序

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<set>
     4 #include<cstring>
     5 #include<cstdlib>
     6 using namespace std;
     7 
     8 int mon_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     9 
    10 class Date{
    11 public:
    12     Date(int y=0,int m=0,int d=0):year(y),month(m),day(d){}
    13     bool isLeap(int year){
    14         return (year%4==0&&year%100!=0)||(year%400==0);
    15     }
    16     /*重载<号排序*/
    17     bool operator<(const Date& b) const{
    18         if(this->year==b.year){
    19             if(this->month==b.month){
    20                 return this->day<b.day;
    21             }
    22             return this->month<b.month;
    23         }
    24         return this->year<b.year;
    25     }
    26 
    27     bool valid(){//判断数据合理性
    28         if(year<1960||year>2059) return false;
    29         if(isLeap(year)){//是闰年
    30             if(month<=0||month>12) return false;
    31             if(month==2) day<=mon_day[month]+1;
    32             return day>=1&&day<=mon_day[month];
    33         }
    34         else{//不是闰年
    35             if(month<=0||month>12) return false;
    36             return day>=1&&day<=mon_day[month];
    37         }
    38     }
    39 
    40 public:
    41     int year;
    42     int month;
    43     int day;
    44 };
    45 /*
    46 class flag{//自定义排序
    47 public:
    48     bool operator()(Date a,Date b){
    49         if(a.year==b.year){
    50             if(a.month==b.month){
    51                 return a.day<b.day;
    52             }
    53             return a.month<b.month;
    54         }
    55         return a.year<b.year;
    56     }
    57 };
    58 */
    59 
    60 set<Date> ss;
    61 void Insert(int y,int m,int d){
    62     Date dd(y,m,d);
    63     if(dd.valid()){
    64         ss.insert(dd);
    65     }
    66 }
    67 
    68 int main(){
    69     int y,m,d;
    70     scanf("%d/%d/%d",&y,&m,&d);
    71     //年月日
    72     Insert(1900+y,m,d);
    73     Insert(2000+y,m,d);
    74     //日月年
    75     Insert(1900+d,m,y);
    76     Insert(2000+d,m,y);
    77     //月日年
    78     Insert(1900+d,y,m);
    79     Insert(2000+d,y,m);
    80 
    81     set<Date>::iterator it;
    82     for( it=ss.begin(); it!=ss.end(); it++ ){
    83         printf("%d-%02d-%02d
    ",(*it).year,(*it).month,(*it).day);
    84     }
    85     return 0;
    86 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    docker 001 简介
    Golang 学习笔记 003 标识符、变量和常量
    Golang 学习笔记 002 第一个 go 程序
    Golang 学习笔记 001 环境部署
    nginx配置url中带问号的rewrite跳转
    北京市图书馆免费入口
    编译安装Python3
    Python—进程、线程、协程
    Python—I/O多路复用
    Python—Socket
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10345620.html
Copyright © 2020-2023  润新知