题目内容:
大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 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 分钟。现给定两对字符串,请帮助福尔摩斯解码得到约会的时间。
输入格式:
输入在 4 行中分别给出 4 个非空、不包含空格、且长度不超过 60 的字符串。
输出格式:
在一行中输出约会的时间,格式为 DAY HH:MM
,其中 DAY
是某星期的 3 字符缩写,即 MON
表示星期一,TUE
表示星期二,WED
表示星期三,THU
表示星期四,FRI
表示星期五,SAT
表示星期六,SUN
表示星期日。题目输入保证每个测试存在唯一解。
输入样例:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
结尾无空行
输出样例:
THU 14:04
结尾无空行
Java代码实现:
一开始的思路很固化,暴力突破,代码臃肿冗长,虽然输入题目给出的例子输出结果一样,但提交没分:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = new String(sc.next());
String str2 = new String(sc.next());
String s1 = str1 + str2;
char[] c1 = new char[s1.length()];
String str3 = new String(sc.next());
String str4 = new String(sc.next());
String s2 = str3 + str4;
char[] c2 = new char[s2.length()];
int Day = 0;
int Hour = 0;
int num1 = 0;
int num2 = 0;
for (int i = 0; i < s1.length(); i++) {
c1[i] = s1.charAt(i);
}
for (int i = 0; i < s2.length(); i++) {
c2[i] = s2.charAt(i);
}
for (int i = 0; i < s1.length(); i++) {
if (c1[i] >= 'A' && c1[i] <= 'Z') {
for (int j = i + 1; j < s1.length(); j++) {
if (c1[j] == c1[i]) {
num1++;
if (num1 == 1) {
Day = c1[i] - 'A' + 1;
break;
}
if (num1 == 2) {
Hour = c1[i] - 'A' + 1;
break;
}
}
}
}
if (num1 == 2)
break;
}
for (int i = 0; i < s2.length(); i++) {
if (c2[i] >= 'A' && c1[i] <= 'z') {
for (int j = i + 1; j < s2.length(); j++) {
if (c2[j] == c2[i]) {
num2 = j;
break;
}
}
}
if (num2 != 0)
break;
}
switch (Day) {
case 1:
System.out.print("MON ");
break;
case 2:
System.out.print("TUS ");
break;
case 3:
System.out.print("WED ");
break;
case 4:
System.out.print("THU ");
break;
case 5:
System.out.print("FRI ");
break;
case 6:
System.out.print("SAT ");
break;
case 7:
System.out.print("SUN ");
break;
}
switch (Hour) {
case 1:
System.out.print("10:");
break;
case 2:
System.out.print("11:");
break;
case 3:
System.out.print("12:");
break;
case 4:
System.out.print("13:");
break;
case 5:
System.out.print("14:");
break;
case 6:
System.out.print("15:");
break;
case 7:
System.out.print("16:");
break;
case 8:
System.out.print("17:");
break;
case 9:
System.out.print("18:");
break;
case 10:
System.out.print("19:");
break;
case 11:
System.out.print("20:");
break;
case 12:
System.out.print("21:");
break;
case 13:
System.out.print("22:");
break;
case 14:
System.out.print("23:");
break;
}
if (num2 >= 0 && num2 <= 9) {
System.out.print("0" + num2);
} else {
System.out.print(num2);
}
}
}
漏洞如下:
1.应该在第二串中寻找与第一串相同的,而不应该把第一二串合起来寻找
2.寻找Hour时应考虑数字字符,即0-9点
优化后代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String[] weeks = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
String line01 = s.nextLine();
String line02 = s.nextLine();
String line03 = s.nextLine();
String line04 = s.nextLine();
// 1.找出周几和第几钟头
boolean isFindWeek = false;
boolean isFindHour = false;
for (int i = 0; i < line01.length() && i < line02.length(); i++) {
char ch1 = line01.charAt(i);
char ch2 = line02.charAt(i);
if (ch1 == ch2) { // 字符相同
// 1.1 找周几。
if (!isFindWeek && ch2 >= 'A' && ch2 <= 'G') { // 没找到周几,且字符在A-G之间
System.out.print(weeks[ch2 - 65] + " ");
isFindWeek = true; // 找到了周几
continue; // 跳过该循环,跳过line01的当前字母
}
// 1.2 找小时。
if (isFindWeek && !isFindHour) { // 找到了周几,且没有找到小时
if (ch2 >= '0' && ch2 <= '9') {
System.out.printf("%02d:", (ch2 - 48));
isFindHour = true;
} else if (ch2 >= 'A' && ch2 <= 'N') {
System.out.print((ch2 - 55) + ":");
isFindHour = true;
}
}
}
}
// 2.找出分钟
for (int i = 0; i < line03.length() && i < line04.length(); i++) {
char ch = line03.charAt(i);
char ch2 = line04.charAt(i);
if (ch == ch2 ) { // 相同的英文字母
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
System.out.printf("%02d", i); // 这一句是格式化输出,补0
return;
}
}
}
}
}