编写程序计算如下数列的值:
1-2+3-4…966
其中,所有数字为整数,从1开始递增,奇数为正,偶数为负
解法1:
s = 0
for i in range(1, 967):
if i % 2 == 0:
s -= i
else:
s += i
print(s)
解法2:
s = 0
count = 1
while count <=966:
if count%2 == 0:
s -= count
else:
s += count
count += 1
print(s)