Java练习 SDUT-2174_回文时间

回文时间

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

  HH 每天都会熬夜写代码,然后很晚才睡觉,但是每天早晨六点多必须要刷卡出宿舍,这就导致了必须在某些课上睡一会才能保证充沛的体力,当然某些重要的课是不能睡掉的,而某些课是可以睡的,比如《中国传统文化》,但是睡觉是不能被老师发现的,否则......他会以让你重修两年来威胁你。已知老师会在电子表上显示的时间为回文(例如:15:51)的时候来检查有没有人在睡觉,所以必须要在那个时间之前醒来。现在,给出 HH 开始睡觉的时间,你要帮她计算出下一个回文时间。

Input

输入包含多组测试数据,对于每组测试数据:
输入只有一行为一个字符串,字符串格式为"HH:MM",HH 和 MM 都为两位数字(00 ≤ HH ≤ 23, 00 ≤ MM ≤ 59)。

Output

对于每组测试数据,输出只有一行为下一个回文时间。

Sample Input

12:21
23:59

Sample Output

13:31
00:00

Hint

Source

qinchuan

题解:根据输入时间分类讨论就可以了。

import java.net.StandardSocketOptions;
import java.util.*;

public class Main
{
	static public void main(String[] args)
	{
		Scanner cin = new Scanner(System.in);
		int a,b,c;
		String s;
		while(cin.hasNextLine())
		{
			s = cin.nextLine();
			a = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0');
			b = (s.charAt(3) - '0') * 10 + (s.charAt(4) - '0');
			c = (s.charAt(1) - '0') * 10 + (s.charAt(0) - '0');
			f(a,b,c);
		}
		cin.close();
	}
	static void f(int a,int b,int c)
	{
		if(a==23)
		{
			if(c>b)
				System.out.printf("%02d:%02d
",a,c);
			else
				System.out.printf("00:00
");
		}
		else if(a>=6&&a<=9)
			System.out.printf("10:01
");
		else if(a>=16&&a<=19)
			System.out.printf("20:02
");
		else
		{
			if(c>b)
				System.out.printf("%02d:%02d
",a,c);
			else
			{
				a++;
				b = 0;
				c = a % 10 * 10 + a / 10;
				f(a,b,c);
			}
		}
	}
}
原文地址:https://www.cnblogs.com/luoxiaoyi/p/9808574.html