devLupin / algorithm

PS
1 stars 0 forks source link

Permutation: 정렬된 수열에서 조합 뽑기 #21

Open devLupin opened 1 year ago

devLupin commented 1 year ago

Permutation


01. next_permutation(iter.begin(), iter.end())

02. prev_permutation(iter.begin(), iter.end())

03. usage

#include <algorithm>
using namespace std;

vector<int> a, b;    // a는 오름차순, b는 내림차순 정렬로 가정

do {
        for(int n : a)  cout << n << ' ';
} while (next_permutation(a.begin(), a.end()));

do {
        for(int n : b)  cout << n << ' ';
} while (prev_permutation(b.begin(), b.end()));
devLupin commented 7 months ago
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(void) {
    vector<int> num{ 1,2,3,4 };
    vector<int> tmp{ 1,1,0,0 };

    do {
        for (int i = 0; i < num.size(); i++)
            if (tmp[i] == 1) cout << num[i] << ' ';
        cout << '\n';
    } while (prev_permutation(tmp.begin(), tmp.end()));

    return 0;
}