版权声明:本文为博主原创文章,转载请注明原博客地址 https://blog.csdn.net/qunqunstyle99/article/details/82903333
题目:福尔摩斯的约会 (20 分)
大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 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
代码实现及分析
这个题目不难,但是有点小复杂,很容易因为小的疏忽无法满分,敲的时候一定要小心加小心。涉及到了字符串的拆解和判断。
package PATY;
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner in = new Scanner(System.in);
String one =in.nextLine();
String two =in.nextLine();
String three =in.nextLine();
String four =in.nextLine();
char one_1[]=one.toCharArray();
char two_1[]=two.toCharArray();
char three_1[]=three.toCharArray();
char four_1[]=four.toCharArray();
int time=0;
int count=0;//统计输出日期还是小时
Loop:while (time<one_1.length||time<two_1.length){
if(one_1[time]==two_1[time]){
if(count==0){
switch (one_1[time]){
case 'A':System.out.print("MON ");count++;break;
case 'B':System.out.print("TUE ");count++;break;
case 'C':System.out.print("WED ");count++;break;
case 'D':System.out.print("THU ");count++;break;
case 'E':System.out.print("FRI ");count++;break;
case 'F':System.out.print("SAT ");count++;break;
case 'G':System.out.print("SUN ");count++;break;
default:break;
}
}else {
if(one_1[time]>=48&one_1[time]<57){
//emmmm,这个地方输出0加一个数,我一开始输出的是空格,卡了我好久,卧槽,太马虎了
System.out.print("0"+one_1[time]+":");break Loop;}
if(one_1[time]==57){
System.out.print(one_1[time]+":");break Loop;}
if(one_1[time]>=65&one_1[time]<=78){
//输出是一个ascll码的位数,所以要转成int
int temp =one_1[time];
System.out.print(temp-55+":");
break Loop;
}
}
}
time++;
}
time=0;
BOOP: while (time<three_1.length||time<four_1.length){
if(three_1[time]==four_1[time]){
//因为不知道是不是只有小写字母有用,我也判断了大写的字符
if((three_1[time]>=97&three_1[time]<=122)||(three_1[time]>=65&three_1[time]<=90)){
if(time<10)
System.out.print("0"+time);
else
System.out.print(time);
break BOOP;
}
}
time++;
}
}
}