题目描述1:求1+2+3+…+n
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
Python测试:
// An highlighted block
class Solution:
def __init__(self):
self.all_sum = 0
def Sum_Solution(self, n):
# write code here
def getsum(n):
self.all_sum += n
n -= 1
return n > 0 and self.Sum_Solution(n)
getsum(n)
return self.all_sum
if __name__ == "__main__":
a = Solution()
b = 5
print(a.Sum_Solution(b))
题目描述2:不用加减乘除做加法
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
Python测试:
// An highlighted block
class Solution:
def Add(self, num1, num2):
# write code here
s = []
s.append(num1)
s.append(num2)
return sum(s)
if __name__ == "__main__":
a = Solution()
b = 5
c = 6
print(a.Add(b, c))
总结
求1+2+3+…+n:
https://blog.csdn.net/qq_38441207/article/details/88776967
不用加减乘除做加法:
https://blog.csdn.net/qq_38441207/article/details/88777006