본문 바로가기
백준 알고리즘/구현

보물상자 비밀번호

by paysmile 2022. 4. 24.

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

 

SW Expert Academy

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

swexpertacademy.com

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;
int n, k;
string map;

int main(void) {
	int testcase;
	int index = 1;
	
	cin >> testcase;
	for (; testcase > 0; testcase--) {
		int answer;
		cin >> n >> k;

		cin >> map;
		vector<string> num;
		int K = n / 4;
		for (; K > 0; K--) {
			int count = n / 4;
			for (int ii = 0; ii < n; ii += count) {
				int id = ii;
				string tp;
				for (int cn = count; cn > 0; cn--) {
					tp += map[id];
					id++;
				}
				num.push_back(tp);
			}

			int tmp = map[n - 1];
			for (int ii = n-2; ii >=0; ii--) {
				map[ii + 1] = map[ii];
			}
			map[0] = tmp;
		}

		vector<int> v;
		for (int i = 0; i < num.size(); i++) {
			int n = 0;
			int loc = 1;
			for (int j = num[i].size()-1; j >=0 ; j--) {
				if (num[i][j] == '0') n = n + loc * 0;
				else if (num[i][j] == '1') n = n + loc * 1;
				else if (num[i][j] == '2') n = n + loc * 2;
				else if (num[i][j] == '3') n = n + loc * 3;
				else if (num[i][j] == '4') n = n + loc * 4;
				else if (num[i][j] == '5') n = n + loc * 5;
				else if (num[i][j] == '6') n = n + loc * 6;
				else if (num[i][j] == '7') n = n + loc * 7;
				else if (num[i][j] == '8') n = n + loc * 8;
				else if (num[i][j] == '9') n = n + loc * 9;
				else if (num[i][j] == 'A') n = n + loc * 10;
				else if (num[i][j] == 'B') n = n + loc * 11;
				else if (num[i][j] == 'C') n = n + loc * 12;
				else if (num[i][j] == 'D') n = n + loc * 13;
				else if (num[i][j] == 'E') n = n + loc * 14;
				else if (num[i][j] == 'F') n = n + loc * 15;
				loc *= 16;
			}
			v.push_back(n);
		}

		sort(v.begin(),v.end());
		v.erase(unique(v.begin(), v.end()),v.end());
		answer = v[v.size()-k];
		cout << "#" << index << " " << answer << "\n";
		index++;
	}
}

'백준 알고리즘 > 구현' 카테고리의 다른 글

미생물 격리 C++  (0) 2022.04.25
무선 충전 C++  (0) 2022.04.25
벽돌 깨기 C++  (0) 2022.04.24
특이한 자석 C++  (0) 2022.04.23
활주로 건설 C++  (0) 2022.04.23