[BOJ] 15655. N과M(6)

Feb 24, 2019


https://www.acmicpc.net/problem/15655

재귀를 사용해서 풀었습니다.

vector를 사용해서 넣어주고 빼주고 했습니다~

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
int arr[9];
vector <int> v;
void go(int x, int cnt) {
    if (cnt == m) {
        for (int i = 0; i < m; i++) {
            cout << v[i] << ' ';
        }cout << '\n';
        return;
    }
    for (int i = x + 1; i <= n; i++) {
        v.push_back(arr[i]);
        go(i, cnt + 1);
        v.pop_back();
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
    }
    sort(arr, arr + n + 1);
    for (int i = 1; i <= n; i++) {
        v.push_back(arr[i]);
        go(i, 1);
        v.pop_back();
    }
}
  • 혼잣말