Recursive algorithm 1

Source: Internet
Author: User

Today I learned the recursive algorithm, the following topic is the understanding of recursion

&1. Ask the nth student how large the topic describes

Example 2.1 There are n students sitting together
Q How old is the nth student? He said he was 2 years older than the n-1 student.
When asked about the age of n-1, he said he was 2 years older than the first n-2 student.
..........................................................
Ask the 2nd student, say 2 years older than the 1th student.
Finally asked the 1th student, he said is 10 years old.
How old is the nth student?

Input

Input n

Output

Output The age of the nth student

Sample input

5

Sample output

18

Idea: (key point for the first student's age)

The 1th student is 10 years old (each student is two years older than the previous student)

N Age

1 10 (When N=1 is age=10, this condition should be listed separately)

2 Age (2-1) +2

3 Age (3-1) +2

...           ...

N Age (n-1) +2

#include <iostream>using namespace std;int FAC (int a) {    int age ;    if (a==1) {return age=10;}    else return FAC (A-1) +2;} int Main () {int n; cin>>n; cout<<fac (n) <<Endl; return 0;}    

&2.fibonacci

Title Description

Example 5.2 asks for the Fibonacci sequence problem.
-The Fibonacci sequence refers to such a sequence:
─0, 1, 1, 2, 3, 5, 8, 13, 、......
─f[0]=0, F[1]=1, f[i]=f[i-1]+f[i-2], i>=2;
First n Fibonacci number of programming outputs

Input

Input n

Output

The first n Fibonacci number of outputs

Sample input

5

Sample output

0

1

1

2

3

Ideas:

F[5]=F[4]+F[3];

F[4]=F[3]+F[2];

F[3]=F[2]+F[1];

F[2]=f[1]+f[0];(here to end, and F[1]. F[2] of the value know, so the first sub-case discussion)

#include <iostream> using namespace std; int f (int x) {     if (x = = 0) {         return 0;     }     else if (x = = 1) {       return 1;} else return F (x-1) +f (X-2);} int main () {int n; Cin >&G T n; for (int i = 0; i < n; i++) {cout << f (i) << Endl;} return 0;}   

&3. Hanoi Tower Problem

Title Description

Hanoi (Hanoi) Tower problem: There is a Vatican tower in ancient times, there are three seats in the tower A, B, C,a seats on the n plates, the size of the plate, large in the lower, small on the (). A monk wanted to move the n plates from block A to block C, but allowed to move only one plate at a time, and in the course of the move, the plates on the 3 seats remained on the market and the small plates were on. In the move process can take advantage of block B, require printing the steps to move. If there is only one plate, you do not need to use block B to move the plate directly from a to C.

Input

Enter the number of plates N

Output

To output a plate with minimal movement

Sample input

3

Sample output

1:a->c

2:a->b

3:c->b

4:a->c

5:b->a

6:b->c

7:a->c

Ideas:

(In a, there are n stacked pieces of wood, the n-1 as a whole, the nth as the other whole)

The first step: Move the n-1 from A to B, at which point N is still in A;

The second step: the N in a is moved to C, n-1 not move;

Step three: Move the n-1 in B to C and complete the heap tower at this time.

#include <iostream>using namespace std;int i=1; void Hannoi (int n,char a,char B,char c) {    if (n==1     {        cout<<i<< ":" <<a<< "<<c<<Endl;        i++;} else {Hannoi (n-1, a,c,b); cout<<i<< ": <<a<<" <<c<< Endl; i++; Hannoi (n-1, b,a,c);}} int Main () {int n; char one= ' A ', two= ' B ', three= ' C '; cin>>N; hannoi (n,one,two,three); return 0< c19>;}               

Recursive algorithm 1

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.