剑指offer47.求1+2+3+...+n

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/87861438

求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
        res = n
        left = res and self.Sum_Solution(n-1)  # 到0停止
        res += left
        return res

猜你喜欢

转载自blog.csdn.net/sinat_36811967/article/details/87861438