Created
November 5, 2012 02:36
-
-
Save BaiGang/4014988 to your computer and use it in GitHub Desktop.
Generate all permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void swap(int* a, int* b) { | |
int tmp = *a; | |
*a = *b; | |
*b = tmp; | |
} | |
void Perm(int m, int n, int* A) { | |
if (m == n) { | |
for (int i = 0; i < n; ++i) { | |
cout << A[i]; | |
} | |
cout << "\n"; | |
return; | |
} | |
for (int j = m; j < n; ++j) { | |
swap(&A[m], &A[j]); | |
Perm(m+1, n, A); | |
swap(&A[m], &A[j]); | |
} | |
} | |
int main(int argc, char* argv[]) { | |
int n; | |
cin >> n; | |
int * A = new int [n]; | |
for (int i = 0; i < n; ++i) { | |
A[i] = i + 1; | |
} | |
Perm(0, n, A); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment