题目:求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
解题思路:运用递归的思想,终止条件采用逻辑与的短路特性终止递归。具体代码实现如下所示:
public class Solution {
public int Sum_Solution(int n) {
int fab=n;
boolean b=(fab>0)&&((fab+=Sum_Solution(fab-1))>0);
return fab;
}
}