1+2+...+n

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

# -*- coding:utf-8 -*-
class Solution:
	def Sum_Solution(self, n)
		ans = n
		# and 后面不能有=,+=等  如果要有+= 这里建个tmp  下面ans += tmp
		# and短路原理,前面为false,0 等,后面不执行,前面为True,执行后面
		tmp = (ans > 0) and self.Sum_Solution(n - 1)
		ans += tmp
		return ans

猜你喜欢

转载自blog.csdn.net/weixin_44088837/article/details/87970420