Recursive recursive tail recursive C + + implementation of Fibonacci sequence

Source: Internet
Author: User

================================== Declaration ==================================

This article is original, reproduced please indicate the author and source, and ensure the integrity of the article (including this statement).

This article is not amended regularly, in order to ensure that the content is correct, suggest to read the original.

This article link: http://www.cnblogs.com/wlsandwho/p/4205524.html

======================================================================

Do not want to do things, brush a webpage, see the chaos of things on the expansion of the unconscious, this bad problem can not be changed, and wasted a lot of time to brush paste.

======================================================================

Handy to write a small code, feeling written is very simple, but for the exit condition is "1" or "2", is "<=" or "<", but it is more egg pain.

Old people do not want to be more brain, their own deduction on it, I the following code by compiling debugging determined under, no problem.

Guess which time I wrote one on the paper on the spot, I caught the shrimp.

======================================================================

To theory, please read the "Data structure and algorithm analysis C language description" and "Algorithm introduction", I put a code, do not spit groove.

======================================================================

Recursive

1#include <Windows.h>2#include <iostream>3 4 using namespacestd;5 6 long Fibonacci (long LN)7 {8     if(ln<2)9     {Ten         returnLN; One     } A  -     returnFibonacci (ln-1) +fibonacci (ln-2); - } the  - intMain () - { -LONG Ls=fibonacci ( +); +  -     return 0; +}

Tail recursion

1#include <Windows.h>2#include <iostream>3 4 using namespacestd;5 6Long Fibonacci (Long Ln,long la=1, LONG lb=1)7 {8     if(ln==2|| ln==1)9     {Ten         returnLB; One     } A  -     returnFibonacci (ln-1, lb,la+LB); - } the  - intMain () - { -LONG Ls=fibonacci ( +); +  -     return 0; +}

Recursive

1#include <Windows.h>2#include <iostream>3 4 using namespacestd;5 6 long Fibonacci (long LN)7 {8LONG ls=1;9LONG la=1;TenLONG lb=1; One  A      for(LONG lcount=3; lcount<=ln;lcount++) -     { -ls=la+LB; theLa=LB; -lb=LS; -     } -  +     returnLS; - } +  A intMain () at { -LONG Ls=fibonacci ( +); -  -     return 0; -}

Recursive recursive tail recursive C + + implementation of Fibonacci sequence

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.