题目描述

求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

public class Solution {
int result=0;
public int Sum_Solution(int n) {
calculate(n);
return result;
}

//使用递归:只要n不为0,就一直加下去(n+ n-1 + n-2 +.....+1)
public boolean calculate(int n){
result+=n;
return (n!=0) && calculate(n-1);
}
}