Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归调用中--n和n-1之间的递归差异_Java_Recursion - Fatal编程技术网

Java 递归调用中--n和n-1之间的递归差异

Java 递归调用中--n和n-1之间的递归差异,java,recursion,Java,Recursion,我需要有人告诉我这两种代码的区别: public static int test(int n) { if(n>2){ int sum =0; int x=n; if(n%3!=0) sum+=test(x-1); System.out.println(n+" "); sum+=test(x-1)+test(x-2); return sum; } else{

我需要有人告诉我这两种代码的区别:

public static int test(int n)
{
    if(n>2){
        int sum =0;
        int x=n;
        if(n%3!=0)
        sum+=test(x-1);
        System.out.println(n+" ");
        sum+=test(x-1)+test(x-2);
        return sum;
    }
    else{
        System.out.println(n+" ");
        return n+1 ; 
    }
}
    public static void main(String[] args) {
       System.out.println(test(5));
    }

}
以及:

public static int test(int n)
{
    if(n>2){
        int sum =0;
        int x=n;
        if(n%3!=0)
        sum+=test(--x);
        System.out.println(n+" ");
        sum+=test(x-1)+test(x-2);
        return sum;
    }
    else{
        System.out.println(n+" ");
        return n+1 ; 
    }
}
    public static void main(String[] args) {
       System.out.println(test(5));
    }

}
更确切地说,我对下面这句话很好奇
sum+=测试(--x);vs总和+=试验(x-1)它给出了不同的输出,你能告诉我当我们有递归(--x)时,递归是如何工作的吗
编辑:
对于sum+=测试(x-1),它给出了以下结果:
3 2 1 4 3 2 1 2 5 3 2 1 4 3 2 1 2 3 2 1 31我得到了这部分,
但对于sum+=测试(--x),输出为:

如果
x
的值为
10
,则此行
sum+=test(x-1)+test(x-2)
将给出:

  • sum+=测试(9)+测试(8)

  • sum+=测试(8)+测试(7)第二个

为什么?
因为在第二个示例中,调用
--x
将通过递减来更改
x
的值。在第一个示例中,它的值没有改变,因为我们将算术运算的结果作为参数传递(
x-1

使用++x后,x值会改变

sum+=test(--x); //after this x=x-1

sum+=test(x-1); //after this x is still x

产生差异的原因是,
x
用在相关行之后
--x
为您提供与
x-1
相同的值,但它也将
x
的值修改为
x-1
。因此,它将改变该行之后代码的行为。

递归调用中的
--x
x-1
与非递归调用中的
没有区别。事实上,当涉及到递归调用时,与普通调用几乎没有什么区别。例如:

public static int test1(int n)
{
  System.out.println(n+1); 
  return n;
}

public static int test2(int n)
{
  System.out.println(n++); 
  return n;
}

public static int test3(int n)
{
  System.out.println(++n); 
  return n;
}
我在这里调用
System.out.println
,但这并不重要。我可以递归,从被调用方的角度来看,它的工作原理是一样的。参数的表达式在此函数中求值,然后使用参数的值进行调用,完成后,函数将继续执行,直到end或return语句

因此,如果我用
1
作为参数调用这3个函数,结果如下:

function  prints  returns because
test1     2       1       n is never changed in this function
test2     1       2       n is incremented but the result is the value before the action
test3     2       2       n is incremented and since its ++n the new value is the result of the expression
丹尼斯·里奇(Dennis Richie)参与了B和C两种语言的研究,在这两种语言中,像
--n
这样的疯狂语法大多来自于这两种语言

当涉及到函数时,改变它的参数可能是不明智的。它使代码更清晰,尤其是在递归函数中。因此,
test1
将是最好的,但是如果您想要
test3
版本,则制作一个变量将使代码更具可读性:

public static int test3(int n)
{
  int value = n + 1;
  System.out.println(value); 
  return value;
}

现在这更容易理解了,不是吗?

你认为
--x
x-1
有什么作用?为什么这样认为?在这种情况下,解决问题的第一次尝试应该是使用调试器、打印语句或直接手写代码。然后你会自己发现不同之处。一个很好的方法是从谷歌搜索运营商开始。这很可能导致。此外,在表达式出现的语句中使用x之前,“预减量”运算符(-x是x的预减量)对x进行操作。这是(a)C用于在速度非常慢的机器上编写操作系统时的遗留问题,通常值得保存每一个可能的机器周期,(b)它是专门为具有寻址模式的机器编译的,该模式允许一条指令访问内存,并在同一条机器指令中递增或递减该内存中的索引。@arcy确实如此,但由于它没有造成两个代码之间的部分差异,我没有提到这一点。谢谢,我不知道这个逻辑在递归调用中也可以工作