首页 > 试题广场 >

求1+2+3+...+n

[编程题]求1+2+3+...+n
  • 热度指数:370581 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

数据范围:
进阶: 空间复杂度 ,时间复杂度
示例1

输入

5

输出

15
示例2

输入

1

输出

1
推荐
class Solution {
public:
    int Sum_Solution(int n) {
        int ans = n;
        ans && (ans += Sum_Solution(n - 1));
        return ans;
    }
};

编辑于 2015-06-19 17:53:23 回复(107)

python解法:

'''
解法1:sum = n(n+1)/2 = (n**2 + n) / 2
'/'可以用'>>1'实现;
'''
class Solution:
    def Sum_Solution(self, n):
        sum = n ** 2 + n
        return sum >> 1
发表于 2021-02-20 16:41:54 回复(0)
提供一个python最简单的实现(大概
class Solution:
    def Sum_Solution(self, n):
        return sum(range(n + 1))


发表于 2020-09-20 23:52:54 回复(0)
python中还真有这样的方法,一行代码就直接实现了
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return sum(range(1,n+1))


发表于 2020-08-02 22:36:25 回复(0)
方1:构造函数,并利用reduce()函数:
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        def f(x,y):
            return x+y
        return reduce(f, range(1,n+1))
        
方2:利用sum和range
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return sum(range(1,n+1))



发表于 2020-07-27 10:44:58 回复(0)
py只有我一个人投机取巧的吗。。。
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return sum(range(1,n+1))
    


发表于 2020-07-01 23:26:55 回复(0)
用recursion的方法,一步就可以了。
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return n>0 and self.Sum_Solution(n-1)+n

发表于 2020-06-27 13:01:58 回复(0)
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        return sum(range(1,n+1))

发表于 2020-06-23 22:28:51 回复(0)
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        if n == 1:
            return 1
        return self.Sum_Solution(n-1)+n

发表于 2020-06-12 23:04:43 回复(0)
class Solution:
    def Sum_Solution(self, n):
        # 此方法内部还是用了题目说的不能用的方法
        # return sum(range(0, n + 1))
        return n and n + self.Sum_Solution(n - 1)

太不容易了,结果老是差一点,终于通过😂
编辑于 2020-06-07 17:46:38 回复(0)
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return sum(range(n+1))

发表于 2020-05-28 14:43:36 回复(0)
递归加逻辑短路
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return n and n+self.Sum_Solution(n-1)



发表于 2020-05-05 15:29:00 回复(0)
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        return n != 0 and n + self.Sum_Solution((n-1))

发表于 2020-04-11 12:07:15 回复(0)
递归
class Solution:
    def Sum_Solution(self, n):
        return n and n + self.Sum_Solution(n - 1)


发表于 2020-04-04 00:57:35 回复(0)
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        return n and n + self.Sum_Solution(n-1)


s = Solution()
ans = s.Sum_Solution(10)
print(ans)

编辑于 2020-03-03 19:13:34 回复(0)
class Solution:
    def Sum_Solution(self, n):
        # write code here
        if n==1:
            return 1
        if n>1:
            return self.Sum_Solution(n-1)+n
很简单的一个递归调用,求f(n)只需要求n+f(n-1).以此递归f(2)=2+f(1);终止条件f(1)=1
发表于 2020-01-09 15:57:27 回复(0)
借鉴and的短路特性,python 一句话
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return n + (n and self.Sum_Solution(n-1))

发表于 2019-12-20 16:10:53 回复(0)
# -*- coding:utf-8 -*-
# 解题思路:非递归方法,使用内置函数和位运算

class Solution:
    def Sum_Solution(self, n):
        # write code here
        return (pow(n, 2) + n) >> 1


# 递归方法
class Solution:
    def __init__(self):
        self.sum = 0

    def Sum_Solution(self, n):
        # write code here
        def qiuhe():
            self.sum += n
            n > 0 and self.Sum_Solution(n-1)

        qiuhe()

        return self.sum

发表于 2019-12-02 16:50:25 回复(0)
python
短路原理实现递归
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        #利用短路原理
        self.sum = 0
        def loop(n):
            self.sum += n
            #递归停止条件由n决定
            #当n>0时,左侧为True,执行右侧并返回
            #当n==0时,左侧为False,由于and与操作只要有一个False结果即为False,那么右侧结果无需执行,直接返回
            return n and loop(n-1)
        loop(n)
        return self.sum


发表于 2019-11-22 09:58:16 回复(0)
利用逻辑判断的短路特性实现递归
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        return n and n+self.Sum_Solution(n-1)


发表于 2019-11-13 12:53:13 回复(0)
# -*- coding:utf-8 -*-
class Solution:
    def Sum_Solution(self, n):
        # write code here
        return sum(range(n+1))
发表于 2019-10-11 19:35:50 回复(0)

问题信息

难度:
80条回答 130165浏览

热门推荐

通过挑战的用户

查看代码