剑指offer:求和

一、问题描述

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

二、代码实现:

class Solution:
    def Sum_Solution(self, n):
        if n ==0:
            return 0
        else:
            b=n and self.Sum_Solution(n-1)
            a=n+b
        return a

 

posted @ 2021-02-04 17:15  小千北同学超爱写代码  阅读(57)  评论(0编辑  收藏  举报