練習6:
題目:斐波那契數列,
陳述句:
# coding: utf-8
from __future__ import print_function
print ('題目:斐波那契數列,')
def f_list(n):
if n == 1:
print ('Fibonacci數列中,第 1個數是0,數列為[0]')
if n == 2:
print ('Fibonacci數列中,第 2個數是1,數列為[0, 1]')
f_lists = [0, 1]
if n >= 3:
for i in range(2, n):
f_lists.append(f_lists[i-1]+f_lists[i-2])
f_x = f_lists[i-1]+f_lists[i-2]
print ('Fibonacci數列中,第%3i個數是%d,數列為%s' % (i+1, f_x, f_lists))
f_list(1)
f_list(2)
f_list(3)
f_list(10)
執行結果:
題目:斐波那契數列,
Fibonacci數列中,第 1個數是0,數列為[0]
Fibonacci數列中,第 2個數是1,數列為[0, 1]
Fibonacci數列中,第 3個數是1,數列為[0, 1, 1]
Fibonacci數列中,第 10個數是34,數列為[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/71719.html
標籤:Python
上一篇:Python簡單資料型別介紹
