WONDY
2017. 11. 22. 02:32
모든 순열을 출력하는 문제이다.
N이 입력된다. 이때 N은 N개 수열을 뜻하며
수열은 1,2,...,N 로 구성되어 있다. 이때 사전순으로 모든 순열을 출력하면 된다.
[C++ source code]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include<cstdio> #include<vector> #define din1(a) scanf("%d", &a) using namespace std; typedef vector<int> vi; void change(vi& v, int here, int there) { int temp = v[here]; v[here] = v[there]; v[there] = temp; } void permutation(vi v, int depth, int depth_end) { if (depth == depth_end) { for (auto it : v) { printf("%d ", it); } puts(""); return; } for (int i = depth; i < depth_end; i++) { change(v, depth, i); permutation(v, depth + 1, depth_end); } } int main() { int n; din1(n); vi v = vi(n); for (int i = 0; i < n; i++) { v[i] = i + 1; } permutation(v, 0, n); return 0; } | cs |