C語言使用普通循環(huán)方法和遞歸求斐波那契序列示例代碼
#include <stdio.h>
int fac(int x);
int main(void)
{
int n;
scanf("%d", &n);
if (n == 1 || n == 2)
printf("1\n");
else if (n == 3)
printf("2\n");
else
{
int last = 1;
int sum = 2;
for (int i = 3; i < n; ++i)
{
int temp = sum;
sum = sum + last;
last = temp;
}
printf("循環(huán)求出斐波那契序列值:%d\n", sum);
}
int sum2 = fac(n);
printf("遞歸求出斐波那契序列值:%d\n", sum2);
return 0;
}
//遞歸
int fac(int x)
{
static int f[50] = {1,1};
if (x == 1 || x == 2)
return f[x-1];
return f[x-1] == 0 ?
( f[x-1] = fac(x-1) + fac(x-2) ) :
f[x-1] ;
}
上一篇:C++調(diào)用迅雷接口解析XML下載功能(迅雷下載功能)
欄 目:C語言
下一篇:C++類成員構(gòu)造函數(shù)和析構(gòu)函數(shù)順序示例詳細(xì)講解
本文標(biāo)題:C語言使用普通循環(huán)方法和遞歸求斐波那契序列示例代碼
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/3893.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求


