看板 Python 關於我們 聯絡資訊
小弟覺得去判斷計數器的奇偶 項太麻煩了,if要寫得落落長 反正知道各項就是正負正負變 換而已,我會這樣寫: n = 500 #你的n值 sign = 1 #正負號 result = 0 #加總 起始為0 for count in range(1, n + 1): result = result + sign * 1 / count sign = - sign print(result) 這樣簡潔很多吧 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.136.189.90 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1522060155.A.93E.html
froce: 其實就是數學啊。多項式整理後第n項怎麼表示而已。 03/26 19:20
froce: https://ideone.com/NitkkP 03/26 19:20
froce: 其實要寫這種,最建議的就是去學函數式編程。 03/26 19:53
froce: 範例是兩個不同數列都用同一個函數產生各項並累加。 03/26 19:56
froce: 基本上就是數學方程式的表示法 03/26 19:57