문제 링크입니다: https://www.acmicpc.net/problem/15656
15656번: N과 M (7)
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열 같은 수를 여러 번 골라도 된다.
www.acmicpc.net
재귀로 쉽게 풀 수 있는 문제였습니다.
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 = 7; | |
int N, M; | |
int arr[MAX]; | |
int visited[MAX]; | |
void func(int idx) | |
{ | |
if (idx == M) | |
{ | |
for (int i = 0; i < M; i++) | |
{ | |
cout << arr[visited[i]] << " "; | |
} | |
cout << "\n"; | |
return; | |
} | |
for (int i = 0; i < N; i++) | |
{ | |
visited[idx] = i; | |
func(idx + 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); | |
return 0; | |
} |


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