剑指Offer的学习笔记(C#篇)-- 求1+2+3+...+n

题目描述

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

一 . 直接解题吧spa

        芽儿呦,忽然以为,我不说!!code

        该题目用到递归,可是却不存在if等判断语句。blog

class Solution
{
    public int Sum_Solution(int n)
    {
        // write code here
        if(n == 1)
        {
            return 1;
        }
        else
        {
            return n + Sum_Solution(n - 1);
        }
    }
}
相关文章
相关标签/搜索