import java.io.*;
class combination
{
static int cntr = 0;
public static void main(String args[]) throws IOException
{
combination call = new combination();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String : ");
String n = br.readLine();
call.comb("",n);
System.out.println("Number of combinations are : "+cntr);
}
public void comb(String beg, String end) throws IOException
{
if (end.length()<=1)
{
cntr++;
System.out.println(beg+end);
}
else
{
for (int i=0;i<end.length();i++)
{
String n = end.substring(0,i)+end.substring(i+1);
comb(beg + end.charAt(i),n);
}
}
}
}
上述程序提供了特定字符串的组合。
我无法理解递归的调用流程。我想知道上面的递归程序的调用流程。
任何人都可以解释一下吗?
答案 0 :(得分:1)
您的程序会执行所有排列,而不是所有组合。有n! (n阶乘)以及程序使n!调用。递归表示一堆方法调用。请记住,每个递归步骤对应于每个堆栈条目。每个堆栈条目都保留其局部变量。现在让我们一步一步地讨论这个问题。举个例子,让n =“abc”。 length = 3 - &gt; !n = 1 * 2 * 3 = 6次递归调用。当var end.length&lt; = 1时,递归结束。它是基本情况(System.out.println)。在每个基本情况下,我们在递归树中返回1级。
所以流程(通常称为递归树):
Step 1. Recurse Step 2 Recurse Step 3 Base case --> print abc Go 1 level back on the recursion tree Step 4 Step 5 Base case --> print acb Go 1 level back on the recursion tree Go 1 level back on the recursion tree Step 6. Recurse Step 7 Recurse Step 8 Base case --> print bac Go 1 level back on the recursion tree Step 9 Recurse Step 10 Base case --> print bca Go 1 level back on the recursion tree Go 1 level back on the recursion tree Step 11. Recurse Step 12 Recurse Step 13 Base case --> print cab Go 1 level back on the recursion tree Step 14 Recurse Step 15 Base case --> print cba Go 1 level back on the recursion tree Go 1 level back on the recursion tree end.
从这个流程中你可以想象“递归步骤”作为推送操作和“Go 1 level back”作为堆栈上的弹出操作并通过流程/代码。
以下是对呼叫的详细分析,您可以通过调试来理解或了解一些削减器。
Step 1: Recursion Level 1 beg = "" end = "abc" cntr = 0 i = 0 n="bc" Step 2: Recursion Level 2 beg = "a" end = "bc" cntr = 0 i = 0 n = "c" Step 3: Recursion Level 3 beg = "ab" end = "c", It's a base case --> Print beg+end = abc cntr = 0 i = 0 Go back to Recursion level 2 Step 4: Recursion Level 2 beg = "a" end = "bc" cntr = 1 i = 1 Print beg+end = acb cntr = 1 Go back to Recursion level 2 Go back to Recursion level 1 Step 6: Recursion Level 1 beg = "" end = "abc" cntr = 1 i = 1 n = "ac" Step 7: Recursion Level 2 beg = "b" end = "ac" cntr = 2 i = 0 n = "c" Step 8: Recursion Level 3 beg = "ba" end = "c", It's a base case --> Print beg+end = bac cntr = 2 i = 0 n = "c" Go back to Recursion level 2 Step 9: Recursion Level 2 beg = "b" end = "ac" cntr = 3 i = 1 n = "a" Step 10: Recursion Level 3 beg = "bc" end = "a", It's a base case --> Print beg+end = bca cntr = 3 i = 1 n = "c" Go back to Recursion level 2 Go back to Recursion level 1 Step 11: Recursion Level 1 beg = "" end = "abc" cntr = 4 i = 2 n="ab" Step 12: Recursion Level 2 beg = "c" end = "ab" cntr = 4 i = 0 n = "b" Step 13: Recursion Level 3 beg = "ca" end = "b", It's a base case --> Print beg+end = cab cntr = 0 i = 0 Go back to Recursion level 2 Step 14: Recursion Level 2 beg = "c" end = "ab" cntr = 5 i = 1 n = "a" Step 15: Recursion Level 3 beg = "cb" end = "a", It's a base case --> Print beg+end = cba cntr = 6 i = 1 Go back to Recursion level 2 Go back to Recursion level 1 end.
答案 1 :(得分:0)
如果我理解正确,comb
会接受一个字符串,并显示该字符串中的字符可以重新排列的所有方式。例如,如果输入"abc"
,程序将提供以下输出:
abc
acb
bac
bca
cab
cba
Number of combinations are : 6
这里的部分问题是comb
的名字不清楚发生了什么。如果我维护此代码,我会将beg
重命名为combinedChars
,将end
重命名为uncombinedChars
(或者remainingChars
),因为该功能可以通过移动在每个递归级别从end
到beg
的单个字符。该算法基本上是: