# 算法-斐波那契数列

```#include "iostream"
using namespace std;
long long Fibonacci(unsigned int n)
{
if(n <= 0)
return 0;

if(n == 1)
return 1;

return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main()
{
int sum = Fibonacci(8);
cout<<sum<<endl;
getchar();
return 0;
}```

```#include "iostream"
using namespace std;
long long Fibonacci(unsigned int n)
{
int result[2] = {0, 1};
if(n < 2)
return result[n];

long long  fibNMinusOne = 1;
long long  fibNMinusTwo = 0;
long long  fibN = 0;
for(unsigned int i = 2; i <= n; ++ i)
{
fibN = fibNMinusOne + fibNMinusTwo;

fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}

return fibN;
}
int main()
{
long long sum = Fibonacci(8);
cout<<sum<<endl;
getchar();
return 0;
}```

```#include "iostream"
#include <time.h>
using namespace std;
// ====================方法1：递归====================
long long Fibonacci_Solution1(unsigned int n)
{
if(n <= 0)
return 0;

if(n == 1)
return 1;

return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2);
}

// ====================方法2：循环====================
long long Fibonacci_Solution2(unsigned n)
{
int result[2] = {0, 1};
if(n < 2)
return result[n];

long long  fibNMinusOne = 1;
long long  fibNMinusTwo = 0;
long long  fibN = 0;
for(unsigned int i = 2; i <= n; ++ i)
{
fibN = fibNMinusOne + fibNMinusTwo;

fibNMinusTwo = fibNMinusOne;
fibNMinusOne = fibN;
}

return fibN;
}
int main()
{
clock_t start1,finish1,start2,finish2;
double totaltime1,totaltime2;
start1=clock();
long long sum1 = Fibonacci_Solution1(40);
finish1=clock();
totaltime1=(double)(finish1-start1)/CLOCKS_PER_SEC;
cout<<"结果："<<sum1<<endl<<"时间："<<totaltime1<<endl;

start2=clock();
long long sum2 = Fibonacci_Solution2(40);
finish2=clock();
totaltime2=(double)(finish2-start2)/CLOCKS_PER_SEC;
cout<<"结果："<<sum2<<endl<<"时间："<<totaltime2<<endl;

getchar();
return 0;
}```

f(7)问题：

f(6)问题：

96 篇文章36 人订阅

0 条评论

## 相关文章

### Dataset 列表：机器学习研究

In computer vision, face images have been used extensively to develop face recog...

1801

921

471

2608

1923

### 高通Audio中ASOC的machine驱动

ASoC被分为Machine、Platform和Codec三大部分，其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的...

1K4

### java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods\$ jmod list java....

1182

### 高通msm8909耳机调试

1、DTS相应修改： DTS相关代码：kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi： 1 s...

8055

### App Guide相关

##TourGuide https://github.com/worker8/TourGuide

822

### Oracle sqlldr 如何导入一个日期列

1. LOAD DATA INFILE * INTO TABLE test FIELDS TERMINATED BY X'9' TRAILING NULLCO...

1876