I am working on finding All permutations of a 3-Digit Number recursively.
I am tired with making up the following permutation Method:
static int a = 1;
static int b = 2;
static int c = 3;
static int aCount;
static int bCount;
static int cCount;
static void perm(int a, int b, int c)
{
Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 )
if (aCount < 1 && bCount<1 &&cCount<1)
{
aCount++;
perm(a, c, b);
}
else
if (aCount==1 && bCount < 1 && cCount<1)
{
bCount++;
perm(b, a, c);
}
else
if (aCount == 1 && bCount == 1 && cCount < 1)
{
perm(b,c,a);
}
else
if (aCount==1 && bCount==1 && cCount < 1)
{
cCount++;
perm(c, a, b); //c b a
}
else
if (aCount == 1 && bCount == 1 && cCount == 1)
{
perm(c, b, a);
}
}
I tried to Cover ALL cases, with the specifics at each step, and still I get a Stack overflow Exception out of the Blue.
I Appreciate Your Contributions, so Thanks Forwards.