ACWING84. 求1+2+…+n(剑指offer)

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

样例
输入:10

输出:55

class Solution {
public:
    int getSum(int n) {
        int res = 0;
        if(n == 0) return 0;
        res += getSum(n - 1) + n;
        return res;
    }
};
发布了844 篇原创文章 · 获赞 28 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104975754