Game Develop

[Algorithm]Baekjoon 1174번 :: 줄어드는 수 본문

Algorithm/Baekjoon

[Algorithm]Baekjoon 1174번 :: 줄어드는 수

MaxLevel 2025. 2. 2. 20:18

https://www.acmicpc.net/problem/1174

 

 

 

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
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_set>
#include <thread>
#include <atomic>
 
using namespace std;
 
int n;
vector<long long> answer;
 
void DFS(string num)
{
    answer.push_back(stoll(num));
 
    int endNum = num.back() - '0';
 
    for (int i = endNum - 1; i >= 0--i)
    {
        num.push_back(i + '0');
        DFS(num);
        num.pop_back();
    }
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
 
    cin >> n;
 
    for (int i = 9; i >= 0--i)
    {
        string temp = "";
        DFS(temp += i + '0');
    }
 
    sort(answer.begin(), answer.end());
 
    if (n >= 1024cout << -1;
    else cout << answer[n - 1];
}
 
 
cs

 

 

어떤 규칙으로 해야 정확한 순번으로 저장할 수 있을까.. 고민 좀 했었는데, 그냥 어떤 방식이든 줄어드는 수를 구한다음에 오름차순정렬을 해주면 된다는 걸 깨달았다.