求 1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字及条件判断语句 (A?B:C)。 数据范围 1≤n≤50000。 样例 输入:10 输出:55
求 1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字及条件判断语句 (A?B:C)。
1≤n≤50000。
输入:10 输出:55
class Solution { public int sumNums(int n) { boolean flag = (n > 0) && ((n += sumNums(n - 1)) > 0); return n; }}
京公网安备 11010502049817号