牛客小白月赛5 J-时间

题目大意就是给你个时间让你求,比这个时间大的第一个回文时间,和比这个时间小的第一个回文时间。

  一开始傻逼的去模拟时间增加和时间减少,写了半天还是没写对,后来看了题解说打表就行了。不过要注意 23:32的很特殊的,比这个时间大的第一个回文时间是0:0(因为0:0分就是24:0),但是他却是存在t数组的1位置,然后比这个时间小的第一个回文时间存的位置是就是cnt-1位置了。

#include<bits/stdc++.h>
using namespace std;
struct time
{
    int h,m;
}t[86405];

int main()
{
    int sx,cnt=0,h,m,pos=-1,s,x;
    scanf("%d:%d",&h,&m);
    for(int i=0;i<=2;i++)
    {
        if(i==2)
            sx=3;
        else
            sx=9;
        for(int j=0;j<=sx;j++)
            for(int k=0;k<=5;k++)
                for(int l=0;l<=9;l++)
                {
                    if(i==l&&j==k)
                    {
                        t[++cnt].h=i*10+j;
                        t[cnt].m=k*10+l;
                    }
                }
    }
    for(int i=1;i<=cnt;i++)
    {
        if(t[i].h>h)
        {
            pos=i;
            break;
        }
        else
            if(t[i].h==h&&t[i].m>m)
            {
                pos=i;
                break;
            }
    }
    if(h==23&&m==32)//特判一下这个时间,因为这个时间实在是很特殊
    {
        cout<<t[cnt-1].h<<":"<<t[cnt-1].m<<endl;
        cout<<t[1].h<<":"<<t[1].m<<endl;

        return 0;
    }
    if(pos==-1)
    {
        x=cnt;
        s=1;
    }
    else
    {
        s=pos;
        if((t[pos-1].h==h)&&(t[pos-1].m==m))
            x=pos-2;
        else
            x=pos-1;
    }
    cout<<t[x].h<<":"<<t[x].m<<endl;
    cout<<t[s].h<<":"<<t[s].m<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/eason9906/p/11755083.html