时间(time)

 牛客小白月赛5 J 时间(time)

题目:

 链接:https://www.nowcoder.com/acm/contest/135/J
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

         Apojacsleam是一个喜欢特殊时刻的人。

        他定义了一个时刻,若电子表显示ab:ba(24小时制),则该时刻为“回文时刻”(可以有前导零)。例如00:00就是回文时刻。

        给定一个时刻,求此时刻的上一个和下一个回文时刻。
J题附加:00:00就是24:00,没有24:00这一时刻
J题附加:输入可能有前导0,输出不含前导0,例如10:1的意思是10:01,而10:10的输出为10:10 

输入描述:

两个正整数,用“:”隔开,表示小时和分钟,保证输入时间合法。

输出描述:

两行,两个时刻(不含前导0),用“:”隔开,表示上一个时刻和下一个时刻
示例1

输入

复制
09:33

输出

复制
5:50
10:1
示例2

输入

复制
23:32

输出

复制
22:22
0:0

 

思路:

  先将所有回文时刻求出来并保存,再按照时间的顺序排序,然后再查询,分所查询的时刻是回文时刻和不是回文时刻两种情况进行讨论,并注意在最后一个回文时刻后面的情况

代码:

#include<cstdio>
#include<algorithm>
using namespace std;
struct Node
{
    int h,s;
} nds[200];
bool cmp(Node a,Node b){
    if(a.h!=b.h){
        return a.h<=b.h;
    }else{
        return a.s<=b.s;
    }
}
int main()
{
    int top=0;
    for(int i=0; i<60; i++)
    {

        int ge=i%10;
        int shi=i/10;
        int tmp=ge*10+shi;
        if(tmp<24)
        {
            nds[++top].h=tmp;
            nds[top].s=i;
        }
    }
    sort(nds+1,nds+1+top,cmp);
/*
    for(int i=1; i<=top; i++)
    {
        printf("%d: %d %d\n",i,nds[i].h,nds[i].s);
    }
    */
    Node now;
    scanf("%d:%d",&now.h,&now.s);
    //  printf("%d %d\n",a,b);

    for(int i=1; i<=top; i++)
    {
        if(now.h==nds[i].h&&now.s==nds[i].s){

            if(i==1){
                printf("%d:%d\n",nds[top].h,nds[top].s);
                printf("%d:%d\n",nds[2].h,nds[2].s);
            }else if(i==top){
                printf("%d:%d\n",nds[top-1].h,nds[top-1].s);
                printf("%d:%d\n",nds[1].h,nds[1].s);
            }else{
                printf("%d:%d\n",nds[i-1].h,nds[i-1].s);
                printf("%d:%d\n",nds[i+1].h,nds[i+1].s);
            }
            return 0;
        }
    }
   // puts("dddd");
    if(now.h==23&&now.s>32){
        printf("23:32\n0:0\n");
        //puts("hhh");
    }else{
        int id=1;
        for(int i=1;i<=top;i++){
            if(nds[i].h>now.h||(nds[i].h==now.h&&nds[i].s>now.h)){
                id=i;
                break;
            }
        }
        printf("%d:%d\n",nds[id-1].h,nds[id-1].s);
        printf("%d:%d\n",nds[id].h,nds[id].s);
    }



    return 0;
}

猜你喜欢

转载自www.cnblogs.com/longl/p/9352953.html