Rabbit reproduction problem adopts two ways of recursion and loop

Rabbit Breeding Problems: Topic Description

There is a rabbit that gives birth to a rabbit every month from the 3rd month after birth. After the little rabbit grows to the 3rd month, another rabbit is born every month. If the rabbit does not die, ask the rabbit of each month. What is the total?

Enter description:

Enter int type to indicate month

Output description:

Output the total number of rabbits int
Basic idea: This is a recursive problem. Starting from the first month, the number of rabbits in each month is: 1, 1, 2, 3, 5, 8, 13........, it can be seen that from the first month At the beginning of three months, the number of rabbits in each month is the sum of the number of rabbits in the previous two months, which is expressed mathematically as F(month) = F(month-1)+F(month-2).
Method 1 : Use recursion to implement
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while(in.hasNext())
        {
            int i = in.nextInt();
            System.out.println(function(i));
        }
    }
    
    public static int function(int month)
    {
        if(month==1)
            return 1;
        else if(month==2)
            return 1;
        else
            return function(month-1) + function(month-2);
    }
}
Advantages and disadvantages of recursion: The use of recursion can make the code more concise and clear, and the readability is better (not necessarily for beginners), but because recursion requires a system stack, the space consumption is much larger than that of non-recursive code, and, If the recursion depth is too large, there may be insufficient system resources.
Method 2: (Better method, using loop instead of recursion)
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        while(in.hasNext())
        {
            int m1=1,m2=1,temp;
            int month = in.nextInt();
            if(month==1||month==2)
                System.out.println(1);    //输出第一个月和第二个月的兔子数
            else 
            {
                for(int i=3;i<=month;i++)
                {
                    temp = m2;
                    m2 = m1 + m2;
                    m1 = temp;
                }
                System.out.println(m2);    //从第三个月开始的兔子数目
            }
        }
    }
}

Similar question: Soda bottle problem:
Topic description

There is such a puzzle: "A certain store stipulates that three empty soda bottles can be exchanged for one bottle of soda. Xiao Zhang has ten empty soda bottles, how many bottles of soda can she drink at most?" The answer is 5 bottles, the method is as follows : First replace 3 soda bottles with 9 empty bottles, drink 3 full bottles, after drinking 4 empty bottles, exchange 3 bottles for another bottle, drink the full bottle, then there are 2 empty bottles left. Then you ask the boss to lend you a bottle of soda first, drink the full bottle, and return the full bottle to the boss with 3 empty bottles after drinking. If Xiao Zhang has n empty soda bottles, how many bottles of soda can he drink at most? 

Enter description:

 
    

The input file contains at most 10 sets of test data, each of which occupies one line and contains only a positive integer n (1<=n<=100), which represents the number of empty soda bottles in Xiao Zhang's hand. n=0 means end of input and your program should not process this line.

Output description:

 
    

For each set of test data, output a row representing the maximum number of soda bottles that can be drunk. If you can't drink a bottle, output 0.

Example 1

enter

3
10
81
0

output

1
5
40

List: Enter caps: 1, 2, 3, 4, 5, 6, 7, 8.........

          喝的汽水瓶数目:1,1,1,2,2,3,3,4..........

递归方式代码:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
       Scanner in = new Scanner(System.in);
       while(in.hasNext())
       {
            int n = in.nextInt();
            if(n==0)
                break;
             else
                System.out.println(printNumber(n));
        }
    }
    
      public static int printNumber(int n)
      {
           int i = n/3;
           int j = n%3;
           if(i==0&&j==1)
           	    return 0;
           else if(i==0&&j==2)
            	return 1;
           return i + printNumber(i+j);
      }
}

使用循环替代递归:

代码实现:

 public static int printNumber(int n)
      {
          int i,j;
          int sum = 0;
          while(n>=3)
          {
              i = n/3;
              j = n%3;
              sum += i;
              n = i + j;
          }
          if(n==2)
              sum += 1;
          if(n==1)
              sum += 0;
          return sum;
      }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324148768&siteId=291194637