Go语言实现:【剑指offer】求1+2+3+...+n

该题目来源于牛客网《剑指offer》专题。

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

Go语言实现:

func sumSolution(n int) int {
   return (int(math.Pow(float64(n), 2)) + n) >> 1
}
​
public static int Sum_Solution(int n) {
   int sum = n;
   boolean flag = (sum > 0) && ((sum += Sum_Solution(--n)) > 0);
   return sum;
}
原文地址:https://www.cnblogs.com/dubinyang/p/12099380.html