计算1到100的和
2018-08-26 本文已影响0人
Mlotjve
利用递归求解
def sum_1(num):
if num == 1:
return 1
else:
return num + sum_1(num-1)
用 最笨的方法求解:
def sum_2():
sum = 0
for i in range(101):
sum = sum+i
return sum
用python牛逼的sum求解:
def sum_3():
return sum(range(1,101))
用奇偶相加求解:
def sum_4():
value = 0
value_1 = 0
for i in range(1,101):
if i%2 == 0:
value = value + i
else:
value_1 = value_1 + i
return value + value_1