黄色电影一区二区,韩国少妇自慰A片免费看,精品人妻少妇一级毛片免费蜜桃AV按摩师 ,超碰 香蕉

C 語(yǔ)言實(shí)例 – 斐波那契數(shù)列

C 語(yǔ)言實(shí)例 - 斐波那契數(shù)列

C 語(yǔ)言實(shí)例 C 語(yǔ)言實(shí)例

斐波那契數(shù)列指的是這樣一個(gè)數(shù)列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........

這個(gè)數(shù)列從第3項(xiàng)開始,每一項(xiàng)都等于前兩項(xiàng)之和。

實(shí)例 - 輸出指定數(shù)量的斐波那契數(shù)列

#include <stdio.h> int main() { int i, n, t1 = 0, t2 = 1, nextTerm; printf("輸出幾項(xiàng): "); scanf("%d", &n); printf("斐波那契數(shù)列: "); for (i = 1; i <= n; ++i) { printf("%d, ", t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } return 0; }

運(yùn)行結(jié)果:

輸出幾項(xiàng): 10
斐波那契數(shù)列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

實(shí)例 - 輸出指定數(shù)字前的斐波那契數(shù)列

#include <stdio.h> int main() { int t1 = 0, t2 = 1, nextTerm = 0, n; printf("輸入一個(gè)正數(shù): "); scanf("%d", &n); // 顯示前兩項(xiàng) printf("斐波那契數(shù)列: %d, %d, ", t1, t2); nextTerm = t1 + t2; while(nextTerm <= n) { printf("%d, ",nextTerm); t1 = t2; t2 = nextTerm; nextTerm = t1 + t2; } return 0; }

運(yùn)行結(jié)果:

輸入一個(gè)正數(shù): 100
斐波那契數(shù)列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

C 語(yǔ)言實(shí)例 C 語(yǔ)言實(shí)例

相關(guān)文章