網站首頁 美容小常識 享受生活 東方時尚 識真假 高奢 資訊 遊戲攻略 搞笑段子
當前位置:品位站 > 享受生活 > 心理

c語言求斐波那契數列前n項值

欄目: 心理 / 發佈於: / 人氣:2.57W
c語言求斐波那契數列前n項值

斐波那契數列,該數列從第三項開始,每一項都等於前二項只和,根據此特點,編寫一個間單 c語言程序:

#include <stdio.h>

int main(int argc,char **argv)

{

unsigned int t1 = 0

unsigned int t2 = 1

unsigned int nextTerm = 0

unsigned int cnt = (unsigned int)atoi(argv[1])

if(cnt>1000)

cnt = 1000

printf("Fibonacci series:")

for (unsigned int i = 1 i <= cnt i++)

{

printf("%d ", t1)

nextTerm = t1 + t2

t1 = t2

t2 = nextTerm

}

return 0

}

編譯源文件為可執行文件test,執行:

/test 10結果如下:

Fibonacci series:0 1 1 2 3 5 8 13 21 34