python-006求1-2+3-4+5.....99的所有数的和
2021-01-25 04:14
标签:技术 sed print else int sum 图片 src closed python-006求1-2+3-4+5.....99的所有数的和 标签:技术 sed print else int sum 图片 src closed 原文地址:https://www.cnblogs.com/bigdata-6/p/12862322.html 1 # 求1-2+3-4+5.....99的所有数的和(含等式)
2 s = ""
3
4 sum = 0
5 start = 1
6
7 while start :
8 temp = start % 2
9 if temp == 1:
10 if start == 1:
11 s = str(start)
12 else:
13 s = s + "+" + str(start)
14 sum = sum + start
15 else:
16 s = s + "-" + str(start)
17 sum = sum - start
18 start += 1
19
20 print(s)
21 print(start)
22
23
24
25 # 求1-2+3-4+5.....99的所有数的和(不含等式)
26 sum = 0
27 start = 1
28
29 while start :
30 temp = start % 2
31 if temp == 1:
32 sum = sum + start
33 else:
34 sum = sum - start
35 start += 1
36
37 print(sum)
38 print(start)
文章标题:python-006求1-2+3-4+5.....99的所有数的和
文章链接:http://soscw.com/index.php/essay/46631.html