[2-4] 장 두 예는 문제 순열

N 개의 디지털 입력, 모든 순열을 출력한다

[설명]


전반적인 아이디어들을 생성하는 정렬
①-> 다수의 프로그램 일 첫 번째 위치 (N-1)를 교체하는 고른 N-1 자릿수 뒤로.에서
② 생성 번호 N-1의 뒤에 배치.
이 단계는 두 반복된다
(즉, [] 배열에있어서 유용한 방법 때보 플래그 느낌)

[코드]

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10;

int n;
int a[N+10];
int cnt = 0;

void dfs(int k,int m){
    if (k==m+1){
        cout<<++cnt<<":";
        for (int i = 1;i <= m;i++){
            cout<<a[i]<<' ';
        }
        cout<<endl;
        return;
    }
    for (int i = k;i <= m;i++){
        swap(a[k],a[i]);
        dfs(k+1,m);
        swap(a[k],a[i]);
    }
}

int main(){
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    dfs(1,n);
    return 0;
}

추천

출처www.cnblogs.com/AWCXV/p/11618636.html