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

题目描述

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

一 . 直接解题吧

        芽儿呦,突然觉得,我不说!!

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

class Solution
{
    public int Sum_Solution(int n)
    {
        // write code here
        if(n == 1)
        {
            return 1;
        }
        else
        {
            return n + Sum_Solution(n - 1);
        }
    }
}

 

posted @ 2019-06-02 22:06  WeiMLing  阅读(414)  评论(0编辑  收藏  举报