[BOJ] 15654. N과M(5)

Feb 24, 2019


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

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

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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
int arr[9];
bool ch[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 = 1; i <= n; i++) {
        if (ch[i] == false) {
            ch[i] = true;
            v.push_back(arr[i]);
            go(i, cnt + 1);
            ch[i] = false;
            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++) {
        ch[i] = true;
        v.push_back(arr[i]);
        go(i, 1);
        ch[i] = false;
        v.pop_back();
    }
}
  • 혼잣말