문제 링크입니다: https://www.acmicpc.net/problem/15657
15657번: N과 M (8)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 같은 수를 여러 번 골라도 된다. 고른 수열은 비내림차순이어야 한다. 길이가 K인 수열 A가 A1 ≤ A2 ≤ ... ≤ AK-1 ≤ AK를 만족하면, 비내림차순이라고 한다.
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 <algorithm> | |
using namespace std; | |
const int MAX = 8; | |
int N, M; | |
int arr[MAX]; | |
int visited[MAX]; | |
void func(int idx, int start, int cnt) | |
{ | |
if (cnt == M) | |
{ | |
for (int i = 0; i < M; i++) | |
{ | |
cout << arr[visited[i]] << " "; | |
} | |
cout << "\n"; | |
return; | |
} | |
if (idx == N) | |
{ | |
return; | |
} | |
for (int i = start; i < N; i++) | |
{ | |
visited[idx] = i; | |
func(idx + 1, i, cnt + 1); | |
} | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
cin >> N >> M; | |
for (int i = 0; i < N; i++) | |
{ | |
cin >> arr[i]; | |
} | |
sort(arr, arr + N); | |
func(0, 0, 0); | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 15664번 N과 M (10) (0) | 2019.10.22 |
---|---|
백준 15663번 N과 M (9) (0) | 2019.10.22 |
백준 15656번 N과 M (7) (2) | 2019.10.22 |
백준 10867번 중복 빼고 정렬하기 (0) | 2019.10.22 |
백준 16120번 PPAP (0) | 2019.10.22 |