3位数字的递归置换

编程入门 行业动态 更新时间:2024-10-15 02:31:30
本文介绍了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位数字的递归置换

本文发布于:2023-11-29 16:03:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!