-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path47.cpp
More file actions
81 lines (68 loc) · 1.3 KB
/
Copy path47.cpp
File metadata and controls
81 lines (68 loc) · 1.3 KB
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
80
81
#include <vector>
using namespace std;
vector<vector<int>> results;
vector<int> selected_nums;
void backtrack(int N, int sum, int start) {
//❶ 합이 10인경우, 조합을 결과에 추가하고 종료
if (sum == 10) {
results.push_back(selected_nums);
return;
}
for (int i = start; i <= N; ++i) {
//❷ 합이 10보다 적은 경우, 가능한 조합을 계속 확인
if (sum + i <= 10) {
selected_nums.push_back(i);
backtrack(N, sum + i, i + 1);
selected_nums.pop_back();
}
}
}
vector<vector<int>> solution(int N) {
//❸ 숫자 1부터 백트래킹 시작
backtrack(N, 0, 1);
return results;
}
//아래 코드는 테스트 코드 입니다.
#include <iterator>
#include <iostream>
using namespace std;
void init()
{
results.clear();
selected_nums.clear();
}
void print(vector<vector<int>> vec)
{
for(int i = 0; i < vec.size(); i++ ){
copy(vec[i].begin(), vec[i].end(), std::ostream_iterator<int>(cout, " "));
cout << endl;
}
}
int main()
{
print(solution(5));
init();
/*
출력값
1 2 3 4
1 4 5
2 3 5
*/
print(solution(2));
init();
/*
출력값(없음)
*/
print(solution(7));
/*
출력값
1 2 3 4
1 2 7
1 3 6
1 4 5
2 3 5
3 7
4 6
*/
return 0;
}