Data structures and algorithms-string Fibonacci to find the nth item

Source: Internet
Author: User

Title: Define the Fibonacci sequence as follows:
/0 N=0
F (n) = 1 n=1
\ f (n-1) +f (n-2) n=2
Enter N to find the nth item of the sequence in the quickest way.

#include <stdio.h>//recursion int Fibonacci (int n) {    switch (n)    {case    0:        return 0;    Case 1:        return 1;    Default:        return Fibonacci (n-1) + Fibonacci (n-2);    }} Non-recursive int nonrecursionfibonacci (int n) {    int a = 0, b = 1;    int i;    Switch (n)    {case    0:        return 0;    Case 1:        return 1;    Default:        {for            (i = 0; i < n-1; i++)            {                int c = a + B;                A = b;                b = C;            }            return b;}}    } int main () {    int f = Fibonacci (4);    int ff = Nonrecursionfibonacci (4);    printf ("%d,%d", f,ff);    return 0;}

Data structures and algorithms-string Fibonacci to find the nth item

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.