2810. Palindromic Times

2810. Palindromic Times

  Tattah is asleep if and only if Tattah is attending a lecture. This is a well-known formula among Tattah's colleagues.

On a Wednesday afternoon, Tattah was attending Professor HH's lecture. At 12:21, right before falling asleep, he was staring at the digital watch around Saher's wrist. He noticed that the digits on the clock were the same when read from both directions i.e. a palindrome.

  In his sleep, he started dreaming about such rare moments of the day when the time displayed on a digital clock is a palindrome. As soon as he woke up, he felt destined to write a program that finds the next such moment.

  However, he still hasn't mastered the skill of programming while sleeping, so your task is to help him.

Input

  The first and only line of the input starts with a string with the format "HH:MM" where "HH" is from "00" to "23" and "MM" is from "00" to "59". Both "HH" and "MM" have exactly two digits.

Output

  Print the palindromic time of day that comes soonest after the time given in the input. If the input time is palindromic, output the soonest palindromic time after the input time.

Examples
Input
  12:21
Output
  13:31
Input
  23:59
Output
  00:00

说明:这个题目的意思是给你一个时间,输出下一个回文时间,假如输入12:21,在12:21之后的下一个回文时间是13:31,依次类推。一天的时间在00:00到23:59。我的想法是首先就把所有的回文时间存在数组里面,怎么存呢?把所有的时刻转换为分钟数,这样就十分方便知道你输入的时间的下一个回文时间,只需要将输入的时间也转为分钟数,然后和此数组进行比较,很方便拿到下一个回文时间。但是此题的输出有陷阱,比如"10:01",分钟数再转为字符串输出的时候,"01"很重要;比如"02:20","02"很重要。

import java.util.Scanner;

public class Test2810 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String time = sc.nextLine();
        int hh = Integer.parseInt(time.split(":")[0]);
        int mm = Integer.parseInt(time.split(":")[1]);
        int[] arr = new int[100];
        int length = 0;

        // 所有的时钟回文数
        for (int i = 0; i < 24; i++) {
            if (i < 6) {
                arr[length] = i * 60 + 10 * i;
                length++;
            }
            if (i > 9 && i < 16) {
                arr[length] = i * 60 + i % 10 * 10 + i / 10;
                length++;
            }
            if (i > 19) {
                arr[length] = i * 60 + i % 10 * 10 + i / 10;
                length++;
            }
        }

        // 输入的时间转为分钟数
        int num = hh * 60 + mm;

        for (int i = 0; i < length; i++) {
            if (num >= arr[i]) {
                if (num >= arr[length - 1]) {
                    System.out.println("00:00");
                    break;
                } else {
                    continue;
                }
            } else {
                String h = Integer.toString(arr[i] / 60);
                String m = Integer.toString(arr[i] % 60);
                if (Integer.parseInt(h) < 6) {
                    System.out.println("0" + h + ":" + m);
                } else if (Integer.parseInt(m) < 6) {
                    System.out.println(h + ":0" + m);
                } else {
                    System.out.println(h + ":" + m);
                }
                break;
            }
        }
    }
}

 

 

 

转载于:https://www.cnblogs.com/tangxlblog/p/9973688.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值