본문 바로가기

SWEA/삼성SW역량테스트 C++

[SWEA] 보물상자 비밀번호

[문제]

 

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

 

SW Expert Academy

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

www.swexpertacademy.com

[풀이]

 

1.convertToDe() => (문자열 사이즈 /4)를 한 길이의 문자열 4개를 10진수로 바꾸어서 int 배열에 저장한다. (16진수임을 명시 해야 함.)

 

백터에 정수들을 저장 

 

2. rotate() => 입력받은 문자열의 가장 마지막 문자열을 맨 앞으로 가져온다. (한 문자씩 시계방향으로 움직이기 때문에 각 회전마다 변경되는 건 맨 뒤의 문자열 뿐이다.)

 

3. 백터를 오름차순으로 정렬 후, 중복을 제거한다. 

 

*오름차순 정렬 

sort() => algorithm 

 

 

*중복 제거 

vector.erase(unique())

 

*알게 된 점 

stoi가 안된다. stol로 해야함 (int 로 형변환 필수!)

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
 
int n, k;
typedef pair<int, string> pis;
vector<int> res;
string input;
bool compare(const int &a, const int &b) {
    if (a > b) return true;
    return false;
}
void convertToDe()
{
    int size = n/4;
    for (int i = 0; i < n; i+=size)
    {
        int sum = 0;
        // string s = input.substr(i, size);
        for (int j = 0; j < size; j++)
        {
            if ('0' <= input[i+j] && input[i+j] <= '9')
            {
                sum += (input[i+j] - '0') * (int)pow(16, size-j-1);
            }
            else if ('A' <= input[i+j] && input[i+j] <= 'F')
            {
                int tmp = 10 + (input[i+j] - 'A');
                sum += tmp * (int)pow(16, size-j-1);
            }
        }
 
        res.push_back(sum);
    }
}
void rotate() {
    //input을 변형한다.
    //맨 뒷자리 수가 앞으로
    string tmp = input.substr(0, n-1);
    input = input[n-1] + tmp;
 
}
int main()
{
 
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int t;
    cin >> t;
 
    for (int tc = 1; tc <= t; tc++)
    {
        res.clear();
        cin >> n >> k;
  
        cin >> input;
 
        //16진수를 10진수로 바꿔서 정렬
        convertToDe();
        for (int i = 0; i < n/4; i++) {
            //시계방향 회전
            rotate();
            convertToDe();
        }
        
        //res 중복 제거
        sort(res.begin(), res.end(), compare);
        vector<int> :: iterator last;
        last = unique(res.begin(), res.end());
        res.erase(last, res.end());
 
        cout << "#" << tc << " " << res[k-1] << '\n';
    }
 
    return 0;
}