剑指Offer——求1+2+3+...+n

题目描述:

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


分析:

 递归实现。


代码:

1 class Solution {
2 public:
3     int Sum_Solution(int n) {
4         if(n == 0) return 0;
5         return n + Sum_Solution(n - 1);
6     }
7 };

 

posted @ 2017-11-02 23:18  叶建成  阅读(173)  评论(0编辑  收藏  举报