本文介绍了3位数字的递归置换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力递归地查找3位数字的所有排列。
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.
推荐答案您是说您正在尝试递归,但是所有这些行都出现在您的代码中:
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)当然是函数的第一个调用: perm(a,b,c)
and of course the first call of the function: perm(a, b, c)
这要容易得多要做:
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); }更多推荐
3位数字的递归置换
发布评论