问题
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.
回答1:
You're saying you're trying recursion, but all those lines appear in your code:
perm(a, c, b)
perm(b, a, c)
perm(b, c, a)
perm(c, a, b)
perm(c, b, a)
and of course the first call of the function: perm(a, b, c)
It's much easier to do:
static void perm(int a, int b, int c)
{
Console.WriteLine("( {0}, {1}, {2} )", a, b, c);
Console.WriteLine("( {0}, {2}, {1} )", a, b, c);
Console.WriteLine("( {1}, {0}, {2} )", a, b, c);
Console.WriteLine("( {1}, {2}, {0} )", a, b, c);
Console.WriteLine("( {2}, {0}, {1} )", a, b, c);
Console.WriteLine("( {2}, {1}, {0} )", a, b, c);
}
回答2:
For one thing, either of these two cases will lead to infinite recursion:
if (aCount == 1 && bCount == 1 && cCount < 1)
{
perm(b,c,a);
}
And:
if (aCount == 1 && bCount == 1 && cCount == 1)
{
perm(c, b, a);
}
The reason for this is that you don't change aCount
, bCount
or cCount
, so you'll end up triggering the same case repeatedly.
Beyond that, you don't really seem to thinking of the problem recursively - as mentioned in the other answer, all the permutations appear in a single call, so, if you get it to work that way, your recursion depth will 2, which could then essentially involve replacing every recursive call with a print statement, and having a non-recursive function.
For a recursive solution, try to think of a solution where you handle a single character in the current call, and recurse onto the next.
A more detailed explanation, if you can't figure it out:
Start at the first character. Try to swap the current character with each remaining character (including itself, i.e. do nothing). Recurse onto the next character. At the last character, print out all characters. Hint - pass an array and current index to your function.
回答3:
I'll write you the pseudo code:
permutationABC()
{
int n=3;
array SOL[n]/* 1 if you get the element otherwise 0 if you don't get the element , the meaning of SOL is that SOL[0] is 1 if we had got 'a' , otherwise 0. It's the same for the others */
initSolWithAllZero(SOL)
permRecursive(abc,SOL,0,n);
}
permRecursive(SOL,i,n)
{
if i == n then print(SOL)
else
{
for k=0 to n
{
if SOL[k] == 0 then
{
SOL[k]=1 // take
permRecursive(SOL,i+1,n)
SOL[K]=0 // don't take ... go back....
}
}
}
}
The time is O(n*n!) O(n!) is the number of permutation and O(n) is the time that is the printing time.
来源:https://stackoverflow.com/questions/23185925/recursive-permutation-of-a-3-digit-number