劍指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);
        }
    }
}
相關文章
相關標籤/搜索