[英]How this recursive code works.I'm trying to understand the concept but couln't get throuh it
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = sc.nextInt();
}
int a = sc.nextInt();
HelloWorld h = new HelloWorld();
h.find(ar, a);
}
public void find(int ar[], int a) {
System.out.println(a);
for (int i = 0; i < ar.length; i++) {
if (ar[i] == a) {
//System.out.println(a+" "+i);
a = a + 1;
find(ar, a);
}
}
}
}
Input:输入:
2
1
2
1
Output: Output:
1
2
3
3
Can anyone explains me how that recursive code generate the following output?谁能解释我该递归代码如何生成以下 output?
Don't get confused about the recursive code - AFAIK it could be done without recursive code.不要对递归代码感到困惑 - AFAIK 它可以在没有递归代码的情况下完成。
It looks like it sorts the content of the variable ar in a weard kind and removes duplicates.看起来它以磨损类型对变量 ar 的内容进行排序并删除重复项。 But if the value does not exist in ar, it simply prints out a for each element of ar.
但是如果 ar 中不存在该值,它只会为 ar 的每个元素打印出 a。 And if a exists in ar, it prints the next higher value, and if that exists in ar, it prints the next higher value, but if currernt value of a does not exist in ar, just print it for the remaining loop iterations.
如果 a 存在于 ar 中,则打印下一个更高的值,如果存在于 ar 中,则打印下一个更高的值,但如果 ar 中不存在 a 的当前值,则在剩余的循环迭代中打印它。
a at start = 0
result = 0 0 0 0
a at start = 1
result = 1 2 3 3
a at start = 2
result = 2 3 3 3
a at start = -infinite ... 0, 3, 4, ... +infite
result = a a a a
if ar = 1 2 4 5 (order of elements does not matter)
a = 0, result = 0 0 0 0
a = 1, result = 1 2 3 3
a = 2, result = 2 3 3 3
a = 3, result = 3 3 3 3
a = 4, result = 4 5 6 6
a = 5, result = 5 6 6 6
a = 6, result = 6 6 6 6
PS: The recusivity is not really needed, too few parameters change, only a. PS:recusivity 不是很需要,参数变化太少,只有一个。
The content of find without recursivity没有递归的find内容
public void find(int ar[], int a) {
for (int i = 0; i < ar.length; i++) {
System.out.println(a);
if (doesArrayContain(ar, a)) {
//System.out.println(a+" "+i)
a = a + 1;
}
}
}
private boolean doesArrayContain(int ar[], int a) {
for (int idx = 0; idx < ar.length; idx++) {
if (ar[idx] == a) {
return true;
}
}
return false;
}
it appears that you start with看来您从
looping an through an integer 'array of size n' to find a value 'a' if it exists you are then recusing to find 'a+1'循环通过 integer 'array of size n' 找到一个值 'a' 如果它存在,那么你会拒绝找到 'a+1'
main.find [1,2], 1
console >> 1
first recursive [1,2], 2. -------- match for 1
consoles >> 2
second recursive [1,2] 3. -------- match for 2
consoles >> 3
exit second recursive (no match for 3)
third recursive [1,2], 2. -------- match for 2
consoles >> 3
exit third recursive (no match for 3)
loop ends
When you input当你输入
2
1
2
1
then the find method is called as:然后 find 方法被称为:
h.find(ar = [1,2], a = 1);
For the output: Inside find method对于 output:内部查找方法
a = 1
is printed. a = 1
被打印出来。 As passed from the main method.ar = [1,2]
is iterated. ar = [1,2]
被迭代。a[0] = 1 = a
, a is incremented to 2. find(ar = [1,2], a = 2)
is called.a[0] = 1 = a
,所以 a 递增到 2。 find(ar = [1,2], a = 2)
被调用。
a = 2
is printed. a = 2
被打印出来。ar = [1,2]
is iterated. ar = [1,2]
被迭代。a[0] = 1.= a = 2.
a[0] = 1.= a = 2.
a[1] = 2 = a
, a is incremented to 3. find(ar = [1,2], a = 3)
is called.a[1] = 2 = a
, a 递增到 3。 find(ar = [1,2], a = 3)
被调用。
a = 3
is printed. a = 3
被打印出来。 . ar = [1,2]
is iterated. ar = [1,2]
被迭代。a = 3
. a = 3
。a[1] = 2 = a
. a[1] = 2 = a
。 a was turned from 1 to 2 in prev iteration . a = 2+1=3
.a = 2+1=3
。 find(ar = [1,2], a = 3)
is called.find(ar = [1,2], a = 3)
。
a = 3
is printed. a = 3
被打印出来。 . ar = [1,2]
is iterated. ar = [1,2]
被迭代。 Final Output:最终 Output:
1
2
3
3
Point to note:注意点:
If you change value of primitive type variables (like int a
) inside a recursive call, the change will not affect value of a for the parent function.如果您在递归调用中更改原始类型变量(如
int a
)的值,则更改不会影响父 function 的 a 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.