1061 Dating (20 分) java 题解

2021/10/2 14:12:00

本文主要是介绍1061 Dating (20 分) java 题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 -- since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week -- that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

Sample Output:

THU 14:04

题目大意:

在四个字符串中找相等某些字符,转化成特定格式后输出。

解题思路:

主要是理解题目细节,第一个相等字符必须在【A,G】之间,一定不能扩大范围,第二个字符必须在找完第一个字符的索引之后找,且范围一定要在【0,9】或【A,N】之间,注意题意是commmon character ,并不是 capital English letter。第三个字符就常规了,找到相同字符输出索引即可。

java代码:

import java.io.*;
import java.util.*;
	
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s1 = br.readLine();
		String s2 = br.readLine();
		String s3 = br.readLine();
		String s4 = br.readLine();
		StringBuilder builder = new StringBuilder();
		int l1 = s1.length();
		int l2 = s2.length();
		int l = Math.min(l1, l2);
		int flag = 0;
		for(int i = 0; i < l;i++) {
			char c1 = s1.charAt(i);
			char c2 = s2.charAt(i);
			if(c1 >= 'A' && c1 <= 'G') {
				if(c1 == c2) {
					builder.append(convertToString(c1) + " ");
					flag = i + 1;
					break;
				}
			}
		}
		for(int i = flag; i < l;i++) {
			char c = s1.charAt(i);
			char c2 = s2.charAt(i);
			if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'N')) {
				if(c ==c2) {
					int x = convertToInt(c);
					builder.append(String.format("%02d", x) + ":");
					break;
				}
			}
		}
		
		int len1 = s3.length();
		int len2 = s4.length();
		int len = Math.min(len1, len2);
		int index = 0;
		for(int i =0 ;i < len;i++) {
			if(Character.isAlphabetic(s3.charAt(i)) && Character.isAlphabetic(s4.charAt(i))) {
				if(s3.charAt(i) == s4.charAt(i)) {
					index = i;
					break;
				}
			}
		}
		builder.append(String.format("%02d", index));
		System.out.print(builder.toString());
	}
	public static String convertToString(char c) {
		int n = c - 'A';
		String[]s = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
		if(n < 7) {
			return s[n];
		}
		return null;
	}
	public static int convertToInt(char c) {
		if(c >= '0' && c <='9') {
			return c - '0';
		}
		int n = c - 'A';
		return 9 + n + 1;
	}
}

PTA提交截图:



这篇关于1061 Dating (20 分) java 题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程