보물상자 비밀번호
Updated:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int X = sc.nextInt();
for (int tc = 1; tc <= X; tc++) {
int N = sc.nextInt();
int K = sc.nextInt();
char[] arr = new char[N];
String str = sc.next();
for (int i = 0; i < N; i++) {
arr[i] = str.charAt(i);
}
int[] arrs = new int[N];
for (int i = 0; i < N; i++) {
int a = arr[i] - '0';
if (a > 10)
a -= 7;
arrs[i] = a;
}
// System.out.println(Arrays.toString(arrs));
ArrayList<Integer> list = new ArrayList<>();
int T = N / 4;
int tmp = T - 1;
int sum = 0;
int cnt = 0;
for (int k = 0; k < N / 4; k++) {
// System.out.println(Arrays.toString(arrs));
for (int i = 0; i < N; i++) {
if (i % T == cnt) {
sum += (Math.pow(16, tmp) * arrs[i]);
cnt++;
tmp--;
}
if (cnt == T - 1) {
sum += arrs[i + 1];
cnt = 0;
if (!list.contains(sum))
list.add(sum);
sum = 0;
tmp = T - 1;
}
}
int temp = arrs[N - 1];
for (int i = N - 2; i >= 0; i--) {
arrs[i + 1] = arrs[i];
}
arrs[0] = temp;
}
Collections.sort(list);
//System.out.println(list);
System.out.println("#" + tc + " " + list.get(list.size() - K));
}
}
}
Leave a comment