C + + to find the Fibonacci number of the instance Code _c language

Source: Internet
Author: User

Topic: The Fibonacci number is defined as: F (0) =0,f (1) =1,f (n) =f (n-1) +f (n-2) (N>1 and N is an integer)

If you write the Phillips series, it should be:

0 1 1 2 3 5 8 13 21 34 ...

If the 6th item is to be asked, it shall be 8.

Find the nth Xiangfi number.

Input Description: The input data contains no more than 50 positive integer n (0<=n<=46).

Output Description: For each n, calculate its nth Xiangfi number, each result should be a separate row.

Topic Analysis: First of the No. 0 to the 46th Fibonacci number to find out, put in an array, and then directly to the table can be, so no time out.

Reference code:

Copy Code code as follows:

#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;
}


Effect as shown:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.