알고리즘 문제풀이/swea
[SWEA 5658] 보물상자 비밀번호
홍시홍
2020. 4. 18. 01:00
분류 : 구현
요구사항
1. 자물쇠 돌려서 얻을 수 있는 모든 비밀번호 구하기
2. k번째 수 10진수로 출력하기
풀이
자물쇠 돌려서 비밀번호 얻는 것은 deque나 string 혹은 배열을 이용해서 하면된다
나는 string으로 substr을 구하는 방법으로 구하였다
16진수 ->10진수 구하는 것은 for문을 이용하여 구하였다.
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
int n, k;
string str;
bool com(string a, string b) {
if (a > b) return true;
return false;
}
int get_string(char a) {
if (a == '0') return 0;
else if (a == '1')return 1;
else if (a == '2')return 2;
else if (a == '3')return 3;
else if (a == '4')return 4;
else if (a == '5')return 5;
else if (a == '6')return 6;
else if (a == '7')return 7;
else if (a == '8')return 8;
else if (a == '9')return 9;
else if (a == 'A')return 10;
else if (a == 'B')return 11;
else if (a == 'C')return 12;
else if (a == 'D')return 13;
else if (a == 'E')return 14;
else if (a == 'F')return 15;
}
int get_num(string a) {
int num = 1;
int ansnum = 0;
for (int i = a.size() - 1; i >= 0; i--) {
int nownum = get_string(a[i]);
ansnum += (nownum * num);
num *= 16;
}
return ansnum;
}
void solve(string s, int tc) {
string ans;
int rotatenum = (n / 4);
int num = n / 4;
int dir[4] = { 0,1,2,3 };
vector<string> vstr;
int val = 10;
while (val--) {
for (int i = 0; i < 4; i++) {
string now = s.substr(dir[i] * num, num);
vstr.push_back(now);
}
// cout << endl;
char temp = s.back();
auto iter = s.begin();
s.pop_back();
s.insert(iter, temp);
}
sort(vstr.begin(), vstr.end(), com);
int cnt = 1;
string fir = vstr[0];
for (int i = 1; i < vstr.size(); i++) {
if (cnt == k) {
ans = fir;
break;
}
if (fir == vstr[i]) {
continue;
}
else {
fir = vstr[i];
cnt++;
}
}
printf("#%d %d\n", tc, get_num(ans));
}
int main() {
int t;
scanf("%d", &t);
for (int tc = 1; tc <= t; tc++) {
scanf("%d%d", &n, &k);
str.clear();
cin >> str;
solve(str, tc);
}
}