Created
May 27, 2012 12:17
-
-
Save hpcx82/2812657 to your computer and use it in GitHub Desktop.
Use a bitmap(or hashmap), and recursively to get the permutation
This file contains hidden or 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; | |
// permutate recusively - use a bitmap, index as the permutate value. | |
// If the target to permutation value doesn't fit in a bitmap, just use a hashmap. | |
void permutate() | |
{ | |
static int a[] = {0, 0, 0, 0}; | |
static int o[4]; | |
static int oi = -1; | |
for(int i = 0; i < sizeof(a)/sizeof(a[0]); ++i) | |
{ | |
if(a[i] == 0) | |
{ | |
a[i] = 1; | |
oi++; | |
o[oi] = i + 1; | |
permutate(); | |
oi--; | |
a[i] = 0; | |
} | |
} | |
if(oi == 3) | |
{ | |
for(int i = 0; i < 4; i++) | |
cout << o[i] << " "; | |
cout << endl; | |
} | |
} | |
int main() | |
{ | |
permutate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment