break
,由於後面也可能有相同的字母A-G
,A
表明"MON"
,D
表明THU
0-9
或A-N
Math.min(int a, int b)
獲取較短長度,來避免越界A-Z
或a-z
j454HkCTOtFN5uh J65tHkCtOtFN4 &.\6wowpHnv &.\6WOWpH
WED 15:07
上面的樣例能測試注意中涉及到的全部細節。java
/** * Score: 20 * Run Time: 120ms * @author wowpH * @version 1.6 */ import java.util.Scanner; public class Main { public static void main(String[] args) { String[] s = new String[4]; String[] week = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" }; Scanner sc = new Scanner(System.in); for (int i = 0; i < 4; i++) { s[i] = sc.next();// 輸入 } sc.close(); // 星期幾 int i; char ch; int len = s[0].length(); for (i = 0; i < len; i++) { ch = s[0].charAt(i); if (ch == s[1].charAt(i)) { if (ch >= 'A' && ch <= 'G') {// A~G System.out.print(week[ch - 'A'] + " ");// 星期幾 break; } } } // 小時 for (i = i + 1; i < len; i++) { ch = s[0].charAt(i); if (ch == s[1].charAt(i)) { if (Character.isDigit(ch)) {// 0~9 System.out.printf("%02d:", ch - '0'); break; } else if (ch >= 'A' && ch <= 'N') {// A~N System.out.printf("%02d:", ch - 'A' + 10); break; } } } // 分鐘 len = s[2].length(); for (i = 0; i < len; i++) { ch = s[2].charAt(i); if (ch == s[3].charAt(i)) { // A~Z或a~z if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') { System.out.printf("%02d\n", i); break; } } } } }