본문 바로가기
Algorithm/C++

[백준 15657] N과 M (8)

by imagineer_jinny 2022. 9. 28.

15657번: N과 M (8) (acmicpc.net)

 

15657번: N과 M (8)

N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오. N개의 자연수는 모두 다른 수이다. N개의 자연수 중에서 M개를 고른 수열

www.acmicpc.net

 

 

정답 풀이

#include <bits/stdc++.h>
using namespace std;


int n, m;
int a[10];
int t[10];
bool isused[10];
void func(int k, int st) { // 현재 k개까지 수를 택했음.
    if (k == m)
    {
        for (int i = 0; i < m; i++)
        {
            cout << a[t[i]] << ' ';
        }
        cout << '\n';
        return;
    }


    for (int i = st; i < n; i++)
    {
      
        t[k] = i;
        func(k + 1,i);
    }

}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    sort(a, a + n);
    func(0,0);
}

'Algorithm > C++' 카테고리의 다른 글

[백준 1181] 단어 정렬  (0) 2022.09.28
[백준 15663] N과 M(9)  (0) 2022.09.28
[백준 9095] 1,2,3 더하기  (0) 2022.09.28
[백준 1463] 1로 만들기  (0) 2022.09.28
[백준 5648] 역원소 정렬  (0) 2022.09.27

댓글