문제 링크입니다: https://www.acmicpc.net/problem/15650
15650번: N과 M (2)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해야 한다.
www.acmicpc.net
범위가 작기 때문에 쉬운 백트래킹 문제였습니다.
idx가 0일 때와 1 이상일 때를 나누어 생각하면 쉽게 풀 수 있는 문제였습니다.
This file contains hidden or 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> | |
using namespace std; | |
const int MAX = 8; | |
int N, M; | |
int arr[MAX]; | |
void func(int idx) | |
{ | |
if (idx == M) | |
{ | |
for (int i = 0; i < M; i++) | |
{ | |
cout << arr[i] << " "; | |
} | |
cout << "\n"; | |
return; | |
} | |
for (int i = idx == 0 ? 1 : arr[idx - 1] + 1; i <= N; i++) | |
{ | |
arr[idx] = i; | |
func(idx + 1); | |
} | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
cin >> N >> M; | |
func(0); | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 15654번 N과 M (5) (0) | 2019.08.25 |
---|---|
백준 15651번 N과 M (3) (3) | 2019.08.23 |
백준 2775번 부녀회장이 될테야 (0) | 2019.08.11 |
백준 2981번 검문 (6) | 2019.08.08 |
백준 2858번 기숙사 바닥 (2) | 2019.08.08 |