[SWEA] 단순 2진 암호코드

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

1이 나오는 시점을 찾아 그 코드가 암호코드 숫자에 있는지 확인하면서 0을 추가하는 방식으로 해결한다.

JAVA Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
public class Solution {
	static int lastIndex;
	static HashMap<String, Integer> map = new HashMap<>();
	static String[] keys = {"0001101","0011001","0010011","0111101","0100011",
			"0110001","0101111","0111011","0110111","0001011"};
	static int[] values = {0,1,2,3,4,5,6,7,8,9};

	public static void main(String[] args) {

		for(int i=0; i<keys.length; i++) { // map에 암호문과 해당 번호 세팅
			map.put(keys[i], values[i]);
		}
		
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1; tc<=T; tc++) {
			int even = 0;
			int odd = 0;
			int res = 0;
			boolean check = true;
			int cols = sc.nextInt();
			int rows = sc.nextInt();
			for(int i=0; i<cols; i++) {
				String row = sc.next();
				if(row.contains("1") && check) {
					lastIndex = row.lastIndexOf("1");
					String sentence = row.substring(lastIndex-55, lastIndex+1);
					int[] code = new int[8];
					for(int j=0; j<8; j++) {
						String temp = sentence.substring(j*7, j*7+7);
						if ( map.containsKey(temp) ) { // 암호문을 가지고 있다면
							code[j] = map.get(temp);
						}
					}
					odd = 0;
					even = 0;
					res = 0;
					for(int k=0; k<code.length; k=k+2) {
						odd += code[k];
					}
					odd = odd * 3;
					for(int k=1; k<code.length; k=k+2) {
						even += code[k];
					}
					
					if ( ((odd + even) % 10) == 0) {
						for(int k=0; k<code.length; k++) {
							res += code[k];
						}
					}
					System.out.println("#"+tc+" "+res);
					check = false;
				} else {
					row = null;
				}
				
			}
		}
	}
}