[SWEA 5658] 보물상자 비밀번호 (Java)

nnm·2020년 6월 1일
0

SWEA 5658 보물상자 비밀번호

문제풀이

  • 배열 돌리기
  • 4개의 변을 가지고 있기 때문에 주어진 숫자들의 길이 / 4 하여 변 마다 16진수 -> 10진수로 만들기
  • 내림차순 정렬이 필요하다면 set -> list로 옮기는 것이 좋다. Collections.reverseOrder()이 편하기 때문이다.

구현코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.StringTokenizer;

public class Solution {
	
	static HashSet<Integer> set;
	static char[] chest;
	static int N, K, T;
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		
		T = Integer.parseInt(br.readLine());
		
		for(int t = 1 ; t <= T ; ++t) {
			st = new StringTokenizer(br.readLine());
			
			N = Integer.parseInt(st.nextToken());
			K = Integer.parseInt(st.nextToken());
			
			chest = br.readLine().toCharArray();
			set = new HashSet<>();
			
			for(int i = 0 ; i < N - 1 ; ++i) {
				getPassword();
				spinClockwise();
			}

			ArrayList<Integer> arr = new ArrayList<>();
			
			for(int i : set) arr.add(i);
			
			Collections.sort(arr, Collections.reverseOrder());
			
			System.out.println("#" + t + " " + arr.get(K - 1));
		}
	}
	
	private static void spinClockwise() {
		char temp = chest[N - 1];
		
		for(int i = N - 1 ; i > 0 ; --i) {
			chest[i] = chest[i - 1];
		}
		
		chest[0] = temp;
	}
	

	private static void getPassword() {
		for(int i = 0 ; i < N ; i += N / 4) {
			String password_hex = "";

			for(int j = i ; j < i + N / 4 ; ++j) {
				password_hex += chest[j];
			}
			
			int password_dec = Integer.parseInt(password_hex, 16);
			set.add(password_dec);
		}
	}
}	
profile
그냥 개발자

0개의 댓글