题目描述:
大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk
d&Hyscvnm”。大侦探很快就明白了,字条上奇怪的乱码实际上就是约会的时间“星期四 14:04”,因为前面两字符串中第1对相同的大写英文字母(大小写有区分)是第4个字母’D’,代表星期四;第2对相同的字符是’E’,那是第5个英文字母,代表一天里的第14个钟头(于是一天的0点到23点由数字0到9、以及大写字母A到N表示);后面两字符串第1对相同的英文字母’s’出现在第4个位置(从0开始计数)上,代表第4分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。
Java代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char[] chars1 = scanner.nextLine().toCharArray(); char[] chars2 = scanner.nextLine().toCharArray(); char[] chars3 = scanner.nextLine().toCharArray(); char[] chars4 = scanner.nextLine().toCharArray(); int no = 1; String date = ""; int h = 0; int m = 0; for (int i = 0; i < Math.min(chars1.length,chars2.length); i++) { if (chars1[i] == chars2[i] && no == 1 && chars1[i] >= 'A' && chars2[i] <= 'G'){ switch (chars1[i]){ case 'A': date = "MON"; break; case 'B': date = "TUE"; break; case 'C': date = "WED"; break; case 'D': date = "THU"; break; case 'E': date = "FRI"; break; case 'F': date = "SAT"; break; case 'G': date = "SUN"; break; } no++; continue; } if (chars1[i] == chars2[i] && no == 2 && chars1[i] >= '0' && chars1[i] <= '9'){ h = chars1[i] - 48; break; }else if (chars1[i] == chars2[i] && no == 2 && chars1[i] >= 'A' && chars1[i] <= 'N') { h = chars1[i] - 55; break; } } for (int i = 0; i < Math.min(chars3.length,chars4.length); i++) { if (chars3[i] == chars4[i] && (chars3[i] + "").toUpperCase().toCharArray()[0] > 64 && (chars3[i] + "").toUpperCase().toCharArray()[0] < 91){ m = i; break; } } String mf = ""; String hf = ""; if (h < 10) hf = "0" + h; else hf = "" + h; if (m < 10) mf = "0" + m; else mf = "" + m; System.out.print(date + " " + hf + ":" + mf); scanner.close(); } }