递归算法

package cn.tedu;
/**
* 求1+2+3+4...+n,要求不能使用乘除法、for、while、if、else、switch、case
* 等关键字及条件判断判断语句
* @author Administrator John
*
*/
public class Test {
  public static int sum(int n) {
    int sum = n;
    /*
    * 递归终止条件,借助&&的短路,对于A && B
    * A=true,执行B
    * B=false,不执行B
    * 所以如果 n=0时,则不会再进行递归调用
    */
    boolean isCountinue = (n>0) && (sum += sum(--n)) >0;
    return sum;
  }
  public static void main(String[] args) {
    int result = sum(10);
    System.out.println(result);
  }
}

原文地址:https://www.cnblogs.com/ahaijava/p/10071393.html