Algorithm Design and Analysis Recursive Advanced Practice Five Questions

An unknown college student, known as Caigou by Jiang Hu
original author: jacky Li
Email : [email protected]

Time of completion:2023.3.15
Last edited: 2023.3.15

Guide:

Help beginners in algorithm design quickly master algorithm design, learn the idea of ​​recursion, and make Quiet understand that repetition and never directness are the truth!

Table of contents

Advanced Recursion Exercises

Level 1: Ackermann function

Reference Code

Level 2: Fibonacci Sequence

Reference Code

Level 3: Frog jumping steps

Reference Code

Level 4: Monkey eating peach problem

Reference Code

Level 5: The problem of dividing apples into baskets

Reference Code

The author has something to say


Advanced Recursion Exercises

Level 1: Ackermann function

mission details

This level requires you to write a recursive function program based on the formula and output the answer.

related information

The Ackerman function is a double recursive function, and its recursive equation is:

programming requirements

Write a recursive function Acm(n,m)to implement the Acm function shown in the figure below, where m、nis a positive integer. For example: Acm(2,1)=4,Acm(3,3)=16.

Input nand mtwo integers, output Acm(n,m). Returns -1 if nless than 0 or less than 0.m

enter:2 1

output:4

Reference Code

#include <iostream>
using namespace std;
int Acm(int n,int m)
{
    if(n == 1 && m == 0) return 2;
    if(n == 0 && m >= 0) return 1;
    if(n >= 2 && m == 0) return n + 2;
    if(n >= 1 && m >= 1) return Acm(Acm(n - 1, m), m - 1);
    if(n < 0 || m < 0) return -1;
}
int main()
{
    int n, m; scanf("%d%d", &n, &m);
    int ans = Acm(n, m);
    printf("%d", ans);
    return 0;
}

Level 2: Fibonacci Sequence

mission details

This level requires you to implement the Fibonacci sequence with a recursive function.

related information

The formula for the Fibonacci sequence is:

F(1)=1,F(2)=1,F(n)=F(n-1)+F(n-2)

programming requirements

Please use a recursive function to realize the Fibonacci sequence, call the recursive function in the main function, and output the value of the nth item.

The effect is as follows:

enter:3

output:2

Reference Code

#include <iostream>
using namespace std;
int F(int n)
{
    if(n == 1) return 1;
    if(n == 2) return 1;
    if(n >= 3) return F(n - 1) + F(n - 2);
}
int main()
{
   int n; cin >> n;
   cout << F(n) << endl;
   return 0;
}

Level 3: Frog jumping steps

mission details

The task of this level: a little frog, every time he jumps the steps, he can jump one step, or two steps at a time, assuming that there are n steps, this frog has a total of many ways to jump.

topic analysis

Assuming that the little frog stood on the nth step at the end, how did it jump up the last time? You can jump up one level, or you can jump up two levels. Suppose f(n) is the number of ways the frog can jump to n steps.

programming requirements

According to the prompt, supplement the code in the editor on the right to calculate how many total jumps there are in n steps.

Test instruction

The platform will test the code you write. The number of steps you need to input is an integer greater than or equal to 1. If it is less than or equal to 0, it will return -1.

Test input:2

Expected output:2

Test input:5

Expected output:8

Reference Code

#include<iostream>
using namespace std;
int frog(int n)
{
   if(n <= 0) return -1;
   if(n == 1) return 1;
   if(n == 2) return 2;
   if(n >= 3) return frog(n - 1) + frog(n - 2);
}
int main()
{
   int n; cin >> n;
   cout << frog(n) << endl;
   return 0;
}

Level 4: Monkey eating peach problem

mission details

The monkey picked several peaches on the first day, ate half of them that day, and ate one more later. The next morning, he ate half of the remaining peaches and ate one more. Every day thereafter, the monkeys ate half and one of the remaining ones from the previous day. When I wanted to eat again on the 10th day, there was only one peach left. Ask how many peaches were picked on the first day. If it is still eaten in this way, when eating again on the 15th day, there is only one peach left, so how many peaches were picked on the first day?

topic analysis

  • You can think backwards, the first 15day is the last day, and the number of peaches isS(15)
  • It is known S(15)=1that, on the 14day, S(14)it is set to remove half of it and one more S(15).
  • As long as it is based on the relationship between S(n-1)and S(n), it can be solved by recursion S(1).

programming requirements

According to the prompt, supplement the code in the editor on the right, and find out that when you want to eat again on the nth day, there is only one peach left, and the total number of peaches picked on the first day.

Test instruction

When the number of days entered nis less than 1, -1 will be returned. The platform will test the code you write:

Test input:4

Expected output:22

Reference Code

#include<iostream>
using namespace std;
int PeachNumber(int n)
{
    static int sum = 1;
    if(n == 1) return sum;
    else 
    {
        sum = (sum + 1) * 2;
        return PeachNumber(n - 1);
    }
}
int main()
{
    int n; cin >> n;
    if(n < 1) cout << -1 << endl;
    else cout << PeachNumber(n) << endl;
    return 0;
}

Level 5: The problem of dividing apples into baskets

mission details

The task of this level: There is nan apple, which is now divided into kbaskets, each basket cannot be empty, how many ways are there? For example, 7if an apple is in 3a basket, it can be {1,1,5}, but the order is not considered here. For example, we think {1,1,5}that {1,5,1}and {5,1,1}are the same division method.

a way of thinking

  1. Consider the first basket, assuming that ian apple is put in, it cannot be larger than that kn​, because if it is larger than this number, repeated divisions will occur.
  2. The remaining n-iapples continue to be placed in k-1 baskets, and their number cannot be more or less k−1n−i​than the number of the previous basket.
  3. Until k==1.
  • For example, enter 7 3
  • According to this method, the first basket can put 1 or 2
  • When the first basket puts 1, the second basket can put 1 or 2 or 3
  • When the first basket puts 2, the second basket can only put 2 or 3
  • ...
    Basket 1 Basket 2 Basket 3
    1 1 5
    1 2 4
    1 3 3
    2 2 3

input format

Two integers n,k,(6<n≤200,2≤k≤6)separated by a single space.

output format

An integer, that is, different divisions.

Test instruction

The platform will test the code you write:

Test input:7 3

Expected output:4

Reference Code

#include<iostream>
 using namespace std;
 //n个苹果分k筐,now是当前筐最少几个苹果
 int divideNumber(int n,int k,int now)
 {
    int ans = 0;
    if(k == 1) return 1;
    else
    {
        for(int i = now; i <= (n / k); i ++)
            ans = ans + divideNumber(n - i, k - 1, i);
    }
    return ans;
 }

 int main()
 {
     int n, k, now = 1; cin >> n >> k;
     cout << divideNumber(n, k, now)<< endl;
     return 0;
 }

The author has something to say

If you feel that what the blogger said is useful to you, please click to support it, and will continue to update such issues...

Guess you like

Origin blog.csdn.net/weixin_62075168/article/details/129563008