求1+2+3+…+n
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
public class Summation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int result = Sum_Solution(5);
System.out.println(result);
}
public static int Sum_Solution(int n) {
int res = n;
boolean flag = (n > 0) && ((res += Sum_Solution(n - 1)) > 0); // 递归
return res;
}
}