Title Content:
Sherlock Holmes received a strange note: let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. The detective soon understood that the strange random code on the note was actually the date time at 14:04 on Thursday, because the first pair of the same uppercase English letters (case sensitive) in the first two strings was the fourth letter D. On behalf of Thursday; The second pair of identical characters is E , That is the fifth English letter, which represents the 14th hour of the day (so the numbers 0 to 9 and capital letters from 0 to 23 o'clock of the day) A reach N Means); The first pair of identical English letters in the last two strings s Appears on the 4th position (counting from 0), representing the 4th minute. Given two pairs of strings, please help Holmes decode the date.
Input format:
Input four non empty strings with no spaces and a length of no more than 60 in each of the four lines.
Output format:
Output the appointment time in one line in the format DAY HH:MM, where DAY Is a 3-character abbreviation for a week, i.e MON Means Monday, TUE It means Tuesday, WED Means Wednesday, THU Means Thursday, FRI Means Friday, SAT Means Saturday, SUN Means Sunday. Topic input ensures that each test has a unique solution.
Input example:
3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm
No blank lines at the end
Output example:
THU 14:04
No blank lines at the end
Java code implementation:
At the beginning, the idea was very solid, violent breakthrough, and the code was cumbersome and lengthy. Although the example output results given by the input topic were the same, the submission was not scored:
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); } } }
The vulnerabilities are as follows:
1. The same as the first string should be found in the second string, rather than combining the first and second strings
2. Numeric characters, i.e. 0-9 points, shall be considered when looking for Hour
The optimized code is as follows:
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. Find out the day of the week and the hour 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) { // Same character // 1.1 find the day of the week. if (!isFindWeek && ch2 >= 'A' && ch2 <= 'G') { // The day of the week is not found and the characters are between A-G System.out.print(weeks[ch2 - 65] + " "); isFindWeek = true; // Found the day of the week continue; // Skip this loop and skip the current letter of line01 } // 1.2 find an hour. if (isFindWeek && !isFindHour) { // The day of the week was found and no hours were found 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. Find out the minutes for (int i = 0; i < line03.length() && i < line04.length(); i++) { char ch = line03.charAt(i); char ch2 = line04.charAt(i); if (ch == ch2 ) { // Same English letters if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { System.out.printf("%02d", i); // This sentence is formatted output, supplemented by 0 return; } } } } }