Try   HackMD
tags: python-TQC

TQC+ 程式語言Python 510 費氏數列

  1. 題目說明:
    請開啟PYD510.py檔案,依下列題意進行作答,計算費氏數列,並依輸入的正整數回傳費氏數列前n個數值,使輸出值符合題意要求。作答完成請另存新檔為PYA510.py再進行評分。

  2. 設計說明:
    請撰寫一程式,計算費氏數列(Fibonacci numbers),使用者輸入一正整數num (num>=2),並將它傳遞給名為compute()的函式,此函式將輸出費氏數列前num個的數值。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  3. 輸入輸出:
    輸入說明
    一個正整數num (num>=2)

輸出說明
依輸入值num,輸出費氏數列前num個的數值(每個數值後方為一個半形空格)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

#method 1 def compute(n): num = [0,1] if n > 2: for i in range(n-2): num.append(num[-2]+num[-1]) return num n = eval(input()) for i in compute(n): print(i,end=" ") #method 2 def compute(i): if i <= 1: return i else: return(compute(i-2)+compute(i-1)) n = eval(input()) for i in range(n): print(compute(i),end=" ")

https://zh.wikipedia.org/zh-tw/斐波那契数

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →