1076 Wifi password (15 points) Java solution to a problem PAT (Basic Level) Practice (Chinese)

1076 Wifi password (15 points)



Link to the original question: Portal

I, entitled:

Here Insert Picture Description
Here Insert Picture Description

Sample Input 1:
8
A-T B-F C-F D-F
C-T B-F A-F D-F
A-F D-F C-F B-T
B-T A-F C-F D-F
B-F D-T A-F C-F
A-T C-F B-F D-F
D-T B-F C-F A-F
C-T A-F B-F D-F
Output Sample 1:
13224143


Second, resolve:

Ideas:

  Because each line is only a T, so find the location of T is the number two and then move forward, get numbers directly determine the output on the line.

AC Code:
import java.util.Scanner;

/**
 * 1076 Wifi密码 (15分)
 * 
 * @思路:因为每行只有一个T,所以找到T的位置后再往前两位就是编号了,拿到编号直接判断输出就行。
 * @author: ChangSheng 
 * @date:   2019年12月30日 下午9:31:16
 */
public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		s.nextLine();
		for (int i = 0; i < N; i++) {
			String line = s.nextLine();
			int indexOfT = line.indexOf("T");
			char id = line.charAt(indexOfT-2);
			if (id == 'A') System.out.print(1);
			if (id == 'B') System.out.print(2);
			if (id == 'C') System.out.print(3);
			if (id == 'D') System.out.print(4);
		}
	}
}
Published 86 original articles · won praise 104 · Views 6634

Guess you like

Origin blog.csdn.net/weixin_44034328/article/details/103789125