回文时间
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
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String str = sc.nextLine();
String str1[] = str.split(":"); //分割成字符串返回
int hh = Integer.parseInt(str1[0]);
int mm = Integer.parseInt(str1[1]);
Another time = new Another(); //使用类与对象
time.f(hh, mm);
}
sc.close();
}
}
class Another {
void f(int hh, int mm) {
int MM = hh % 10 * 10 + hh / 10; //先计算出hh的回文
if(MM > mm && MM <= 59) { //当回文串满足分钟的要求时,直接输出
mm = MM;
}
else { //首先将小时数加一,再计算回文串
do {
hh = (hh + 1) % 24;
mm = hh / 10 + hh % 10 * 10;
if(mm <= 59) {
break;
}
}while(true);
}
System.out.printf("%02d:%02d\n", hh, mm); //输出格式
}
}
或者:https://blog.csdn.net/Annfan123/article/details/67029061