Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created May 27, 2012 12:17
Show Gist options
  • Save hpcx82/2812657 to your computer and use it in GitHub Desktop.
Save hpcx82/2812657 to your computer and use it in GitHub Desktop.
Use a bitmap(or hashmap), and recursively to get the permutation
#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