AcWing 1547. 约会(Java,官网AC)
原题链接
简单
作者:
码海泛舟
,
2020-06-16 10:30:00
,
所有人可见
,
阅读 846
import java.util.Scanner;
public class Main {
static String[] days = {"", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
static String day;
static int h, m;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();
String s4 = sc.next();
boolean first = true;//是前两个字符串是第一对有效的日期
//前两个字符串
for (int i = 0, j = 0; i < s1.length() && j < s2.length(); i++, j++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(j);
if (c1 != c2) continue;
if (first && validDay(c1)) {//是第一对相同的字符
day = days[c1 - 'A' + 1];
first = false;//第一对相同字符处理完了
} else if (!first && validHour(c1)) {
h = getHour(c1);
break;//第二对处理完直接退出
}
}
//后两个字符串
for (int i = 0, j = 0; i < s3.length() && j < s4.length(); i++, j++) {
char c1 = s3.charAt(i);
char c2 = s4.charAt(j);
if (c1 != c2) continue;
if ((c1 >= 'a' && c1 <= 'z') || (c1 >= 'A' && c1 <= 'Z')) {
m = j;
break;
}
}
//不足两位前面补0
System.out.printf("%s %02d:%02d", day, h, m);
}
private static int getHour(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else {
return 10 + (c - 'A');
}
}
private static boolean validHour(char c) {
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'N')) {
return true;
} else {
return false;
}
}
private static boolean validDay(char c) {
if (c >= 'A' && c <= 'G') {
return true;
} else {
return false;
}
}
}