蓝桥杯 日期问题

问题 G: 日期问题

时间限制: 1 Sec   内存限制: 128 MB
提交: 27   解决: 8
难度: ★☆☆☆☆ | 标签: 基础
[ 提交][ 状态]

题目描述

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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
#include<stdio.h>
struct d
{
    int year;
    int month;
    int day;
} date[3];
int is_com(int i)
{
    int k = i;
    while(k--)
    {
        if(date[k].year==date[i].year
           &&date[k].month==date[i].month
           &&date[k].day==date[i].day)
            return 0;
    }
    return 1;
}//去重
int cmp(const int *a, const int *b)
{
    if((*(struct d*)a).year>(*(struct d*)b).year)
        return 1;
    else if((*(struct d*)a).year==(*(struct d*)b).year)
    {
        if((*(struct d*)a).month>(*(struct d*)b).month)
            return 1;
        else if((*(struct d*)a).month==(*(struct d*)b).month)
        {
            return (*(struct d*)a).day-(*(struct d*)b).day;
        }
        else return 0;
    }
    else
        return 0;
}//结构体三级排序
int isleap(int year)
{
    if(year%400==0||(year%100&&year%4==0))
        return 1;
    return 0;
}//闰年判断
int judge(int i)
{
    int m[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(date[i].year<60)
        date[i].year+=2000;
    else
        date[i].year+=1900;
    if(isleap(date[i].year))
        m[2] = 28;
    if(date[i].month>12)
        return 0;
    if(m[date[i].month]<date[i].day)
        return 0;
    return 1;
}//判断日期是否合法
void initial(int a, int b, int c)
{
    date[0].year = a;
    date[0].month = b;
    date[0].day = c;
    date[1].year = c;
    date[1].month = a;
    date[1].day = b;
    date[2].year = c;
    date[2].month = b;
    date[2].day = a;
}//初始化三种情况
int main()
{
    int a, b, c;
    int i, j;
    char t;
    scanf("%d%c%d%c%d",&a,&t,&b,&t,&c);
    initial(a,b,c);
    qsort(date,3,sizeof(date[0]),cmp);
    for(i = 0 ; i<3 ; i++)
    {
        if(judge(i)&&is_com(i))//日期合法性判断、去重
        {
            printf("%d-%02d-%02d\n",
                   date[i].year,date[i].month,date[i].day);//格式控制输出
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/SWEENEY_HE/article/details/79748725
今日推荐