ALGORITHM/프로그래머스 | 백준 | 삼성 | 카카오

[SWEA] 5658. [모의 SW 역량테스트] 보물상자 비밀번호

SZCODE 2020. 10. 20. 21:40

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRUN9KfZ8DFAUo

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

문제 풀이 :
box 배열에 글자를 하나씩 넣어주고, 시계방향으로 돌리기 위해 for문을 사용합니다.
회전 한 번 할 때마다 list에 넣어줍니다. 각 회전이 종료된 후 list를 정렬시켜 K번째 숫자를 출력합니다.

주의할 점 :

N이 4로 나누어 떨어지지 않을경우, 변의 길이를 자를 때는
if(i+3 <= ss.length()) ss.substring(i,i+3);
else ss.substring(i,ss.length));
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Solution{
	static int N=0, K=0;
	static String s = "";
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			
			N = sc.nextInt();
			K = sc.nextInt();
			s = sc.next();
			char[] box = new char[N];
			ArrayList<Integer> list = new ArrayList();
            
			for (int i = 0; i < N; i++) {
				char c = s.charAt(i);
				box[i] = c;
			}
			
			
			for (int i = 0; i < N/4; i++) {
				
				char temp = box[N-1];
				for (int j = N-1; j > 0; j--) box[j] = box[j-1]; // 박스 회전
				box[0] = temp;

				String ss = "";
				for (int j = 0; j < box.length; j++) { //문자열로 변환
					ss += box[j];
				}
				
				for (int j = 0; j < ss.length(); j+=N/4) {
					String x = ss.substring(j, j+N/4); //변의 길이 자르기
					int change = Integer.parseInt(x,16); //16 -> 10진수로 변환
					if(list.contains(change))continue; //중복제거
					list.add(change);
				
				}
			}	

			Collections.sort(list);
			
			int count = 0;
			for (int i = list.size()-1; i >= 0; i--) {
				count++;
				if(count == K) System.out.println("#"+tc+" "+list.get(i));
				
			}
		}//end of testcase
	}
}