A nice method to display permutations of digits/characters

#include "stdafx.h"
#include<iostream>
#include<stdlib.h>

using namespace System;
using namespace std;

int fact(int n)
{
if(n<=1)
return(1);
else
return(n*fact(n-1));

}

void permute(int num[],int numdig)
{
const int numdig1=numdig;
int num2[120][5];
for(int a=0;a<120;a++)
for(int b=0;b<5;b++)
num2[a][b]=-1;
int count1=fact(numdig);
int i=0;
int count2=0;
int dig=0;
for(i=0;i<=numdig-1;i++)
{
count2=fact(numdig-i-1);
for(int j=0;j<count1;j++)
{
if((j!=0)&&((j%count2)==0))
dig++;
if(dig>=numdig)
dig=0;
if(num2[j][dig]==(-1))
num2[j][dig]=num[i];
else
{
while((num2[j][dig]!=(-1))&&(dig<numdig))
{
dig++;
if(dig>=numdig)
dig=0;
}
num2[j][dig]=num[i];
}
}
dig=0;
}
for(int i=0;i<fact(numdig);i++)
{
for(int j=0;j<numdig;j++)
cout<<num2[i][j];
cout<<endl;
}
}

int main(array<System::String ^> ^args)
{
int a[]={0,2,3,4,2};
permute(a,5);
int i=0;
while(i!=1)
{
cin>>i;
}
return 0;
}
Topic archived. No new replies allowed.