문제 링크입니다: https://www.acmicpc.net/problem/15665
15665번: N과 M (11)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해야 한다.
www.acmicpc.net
앞선 N과 M 문제들과 비슷한 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <map> | |
using namespace std; | |
const int MAX = 7; | |
int N, M; | |
vector<int> v; | |
int arr[MAX]; | |
map<int, bool> visited; | |
void func(int idx, int cnt) | |
{ | |
if (cnt == M) | |
{ | |
for (int i = 0; i < M; i++) | |
{ | |
cout << v[arr[i]] << " "; | |
} | |
cout << "\n"; | |
return; | |
} | |
for (int i = 0; i < v.size(); i++) | |
{ | |
arr[idx] = i; | |
func(idx + 1, cnt + 1); | |
} | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
cin >> N >> M; | |
for (int i = 0; i < N; i++) | |
{ | |
int num; | |
cin >> num; | |
if (!visited.count(num)) | |
{ | |
visited[num] = true; | |
v.push_back(num); | |
} | |
} | |
sort(v.begin(), v.end()); | |
func(0, 0); | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 1799번 비숍 (4) | 2019.10.22 |
---|---|
백준 15666번 N과 M (12) (0) | 2019.10.22 |
백준 15664번 N과 M (10) (0) | 2019.10.22 |
백준 15663번 N과 M (9) (0) | 2019.10.22 |
백준 15657번 N과 M (8) (0) | 2019.10.22 |