求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
/**
* @author Joeson Chan
*/
public class Solution {
public int Sum_Solution(int n) {
boolean tmp = n >0 && (n += Sum_Solution(n - 1)) > 0;
return n;
}
}
本文介绍了一种不使用常规循环和条件判断语句实现的递归求和算法。该方法通过自我调用的方式计算从1累加到n的结果,巧妙地规避了对for、while等关键字的使用,并且也不依赖于if、else等条件判断语句。

被折叠的 条评论
为什么被折叠?



