Title: Seeking the 1+2-3+4-5+6...99 and
Answer:
The first thing you can see from the title is that all the odd numbers except 1 are negative, and all the even numbers are positive, the exact code is as follows:
Method One:
#!/usr/bin/env python#-*-coding:utf-8-*-# author:lisa li# for 1+2-3+4-5+6...99 and count = 1 # set initial value S1 = 0 # receive calculates all even numbers Variable s2 = 0 # receives a variable that computes all cardinality while Count <=: # Remove 1-99 all numbers if count% 2 = = 0: # Take out 1-99 even S1 = s1 + Count # fetched an even number to operate else: # Take out 1-99 of all the odd s2 = s2-count # Out of the odd number to operate s = s1 + s2 # will be calculated by the Add an even value and an odd number of values print (s) # Printing Results count + = 1 # self-increment 1
Output Result:
-11-22-33-44-55-66-77-88-99-1010-1111-1212-1313-1414-1515-1616-1717-1818-1919-2020-2121-2222-2323-2424-2525-2626-2727-282 8-2929-3030-3131-3232-3333-3434-3535-3636-3737-3838-3939-4040-4141-4242-4343-4444-4545-4646-4747-4848-4949-50
View Code
Method Two:
# 1+2-3+4-5+6...99 and s1 = 0 #定义接收所有偶数的变量s2 = 0 #定义接收所有奇数的变量for count in range (1,100): all numbers #取1-99, Gu Tou regardless of tail # Print (count) if count% 2 = = 0: #取1-99 of all even S1 = s1 + Count # will 1-99 all even add else: #取1 99 all Odd Number s2 = S2-count # calculates 1-99 of all odd values s = s1 + s2 #把所有偶数的值和奇数的值进行相加 print (s) #输出结果
Output Result:
-11-22-33-44-55-66-77-88-99-1010-1111-1212-1313-1414-1515-1616-1717-1818-1919-2020-2121-2222-2323-2424-2525-2626-2727-282 8-2929-3030-3131-3232-3333-3434-3535-3636-3737-3838-3939-4040-4141-4242-4343-4444-4545-4646-4747-4848-4949-50
View Code
A simple Python program