【剑指Offer】47.求1+2+3+...+n(Python实现)

题目描述

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

解法一:递归法

# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        result = n
        temp = n > 0 and self.Sum_Solution(n-1)
        result = result + temp
        return result
发布了101 篇原创文章 · 获赞 73 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36936730/article/details/104727683
今日推荐