C++ to find Fibonacci number of instance code
- 2020-04-02 01:56:06
- OfStack
Fibonacci Numbers are defined as f(0)=0,f(1)=1, and f(n)=f(n-1)+f(n-2)(n) > 1 and n is an integer.)
If the fischler sequence is written, it should be:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
If you take the sixth term, it should be eight.
Find the NTH term.
Input description: the input data contains no more than 50 positive integers n(0) < = n < = 46).
Output description: for each n, calculate the NTH term of the fisher number, each result should be a separate line.
Figure out the Fibonacci Numbers for items 0 through 46, put them in an array, and then look them up in the table.
Reference code:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main(int argc,char * argv[])
{
int a[47];
a[0]=0;
a[1]=1;
for(int i=2;i<=46;i++)
{
a[i]=a[i-1]+a[i-2];
}
int n;
while(cin>>n)
{
cout<<a[n]<<endl;
}
system("pause");
return 0;
}
The effect is as follows:
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201310/20131022152036025.png" >