zoukankan      html  css  js  c++  java
  • 蓝桥杯 日期问题

    题意:

    问题描述
      小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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
    分析:
    1、先按对应的格式将年份补充完整,若A < 60,一定是2000年后的,否则是1900年后的。
    2、再判断月份是否合法,若合法,再根据该月份看日期是否合法。
    3、注意月份和日期要大于等于1,而且最后合法的年月日要去重。
    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<sstream>
    #include<cstdlib>
    #include<iostream>
    #include<cmath>
    #include<map>
    #include<set>
    #include<stack>
    #include<vector>
    #include<queue>
    using namespace std;
    const int MAXN = 100 + 10;
    int a, b, c;
    int m1[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int m2[] = {31,29,31,30,31,30,31,31,30,31,30,31};
    struct Node{
        int y, m, d;
        bool operator < (const Node&rhs)const{
            return y < rhs.y || (y == rhs.y && m < rhs.m) || (y == rhs.y && m == rhs.m && d < rhs.d);
        }
    }num[10];
    set<Node> st;
    int cnt;
    bool judge(int y){
        if(y % 400 == 0) return true;
        if(y % 100 != 0 && y % 4 == 0) return true;
        return false;
    }
    bool judge1(){
        if(a < 60){
            num[cnt].y = 2000 + a;
        }
        else{
            num[cnt].y = 1900 + a;
        }
        if(b < 1 || b > 12) return false;
        if(judge(num[cnt].y)){
            if(c >= 1 && c <= m2[b - 1]){
                num[cnt].m = b;
                num[cnt].d = c;
                return true;
            }
            return false;
        }
        else{
            if(c >= 1 && c <= m1[b - 1]){
                num[cnt].m = b;
                num[cnt].d = c;
                return true;
            }
            return false;
        }
    }
    bool judge2(){
        if(c < 60){
            num[cnt].y = 2000 + c;
        }
        else{
            num[cnt].y = 1900 + c;
        }
        if(a < 1 || a > 12) return false;
        if(judge(num[cnt].y)){
            if(b >= 1 && b <= m2[a - 1]){
                num[cnt].m = a;
                num[cnt].d = b;
                return true;
            }
            return false;
        }
        else{
            if(b >= 1 && b <= m1[a - 1]){
                num[cnt].m = a;
                num[cnt].d = b;
                return true;
            }
            return false;
        }
    }
    bool judge3(){
        if(c < 60){
            num[cnt].y = 2000 + c;
        }
        else{
            num[cnt].y = 1900 + c;
        }
        if(b < 1 || b > 12) return false;
        if(judge(num[cnt].y)){
            if(a >= 1 && a <= m2[b - 1]){
                num[cnt].m = b;
                num[cnt].d = a;
                return true;
            }
            return false;
        }
        else{
            if(a >= 1 && a <= m1[b - 1]){
                num[cnt].m = b;
                num[cnt].d = a;
                return true;
            }
            return false;
        }
    }
    int main(){
        scanf("%d/%d/%d", &a, &b, &c);
        cnt = 0;
        if(judge1()){
            ++cnt;
        }
        if(judge2()){
            ++cnt;
        }
        if(judge3()){
            ++cnt;
        }
        sort(num, num + cnt);
        for(int i = 0; i < cnt; ++i){
            st.insert(num[i]);
        }
        for(set<Node>::iterator it = st.begin(); it != st.end(); ++it){
            printf("%d-%02d-%02d
    ", (*it).y, (*it).m, (*it).d);
        }
        return 0;
    }
    

      

  • 相关阅读:
    免费云盘,为什么不用?
    把握linux内核设计思想系列
    volley基本使用方法
    金朝阳——软件測试试题11道题目分享
    可编辑ztree节点的增删改功能图标控制---已解决
    POJ 3370 Halloween treats 鸽巢原理 解题
    Axure RP一个专业的高速原型设计工具
    Linux内核剖析 之 进程简单介绍
    [iOS]怎样在iOS开发中切换显示语言实现国际化
    scp and tar
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/8677733.html
Copyright © 2011-2022 走看看