close
題目概要:
給定一個初始時間,並印出接下來會回文的時間(12:21)。Ps 當連續出現的0可以忽略 Ex 00:01
解題方向:
1. 先以字串的方式讀入資料。
2. 分別把小時、分轉成整數,方便計算。
3. 計算完後再將結果轉成字串去做比較是否回文
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
class uva11309{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int cases=sc.nextInt(); | |
for(int i=0;i<cases;i++){ | |
String st=sc.next(); | |
int h=(st.charAt(0)-48)*10+st.charAt(1)-48; //時 | |
int m=(st.charAt(3)-48)*10+st.charAt(4)-48; //分 | |
while(true){ | |
m++; | |
h=(m/60+h)%24; | |
m=m%60; | |
String temp=Integer.toString(h*100+m); //小時*100+分→轉成字串判斷是否回文 | |
StringBuilder temp2=new StringBuilder(temp); | |
if(temp.equals(temp2.reverse().toString()))break; //當回文就停止 | |
} | |
//Output | |
System.out.println(""+(h/10)+(h%10)+":"+(m/10)+(m%10)); | |
} | |
} | |
} |
文章標籤
全站熱搜