[编程题]等差数列
等差数列
http://www.nowcoder.com/questionTerminal/f792cb014ed0474fb8f53389e7d9c07f
数学思路,转化为等差数列求和,直接用公式计算求解,而不是循环遍历,附python代码
import sys for s in sys.stdin: try: n = int(s) d = 3 a1 = 2 an = a1 + (n-1)*d sums = (a1 + an)/2 * n print(int(sums)) except: print(-1)