Simple way to recursive questions

Topic 1: Selling ducks

A person drives ducks to sell in each village, and sells half of the ducks they drive every time they pass through a village. So he left two ducks after passing through seven villages, and asked how many ducks he drove when he set out? How many ducks are sold through each village?

  • Problem-solving ideas: It is relatively simple to use mathematical ideas to solve such problems. This question is to know how many ducks are left. I don’t know how many ducks there are at the beginning. It is the type that knows the tail without knowing the head. So we need to find the law of F(n) and F(n+m). m is the interval at which the data in the question changes significantly. In this question, m=1, because the data will change every time it passes through a village.
  • Suppose a function F(n), F is the number of ducks after passing n villages, and n is the nth village
  • From the question, we can know that F(7)=2
  • The remaining 2 ducks are sold after passing through 6 villages, F(6)-F(6)/2 -1 = F(7)
  • Convert it to get F(6) = 2(F(7)+1)
  • Follow the logic and analogy F(5) = 2(F(4)+1)
  • Finally we get F(n) = 2(F(n+1)+1)
public class Duck {
    
    
    public static void main(String[] args) {
    
    
    //实参为0,经过0个村子剩下的鸭子数量就是我们出发时的鸭子数量
        int i = countDuck(0);
        System.out.println(i);
        //经过0个村子的鸭子数量减去经过1个村子的鸭子数量,就是第一个村子卖出的鸭子数量
        int duck =countDuck(0)-countDuck(1);
        System.out.println("第1个村子卖出了鸭子的数量"+duck);
    }
    //定义数鸭子的方法
    public static int countDuck(int village){
    
    
        if(village == 7){
    
    
            return  2;
        }else {
    
    
            return  (countDuck(village+1)+1)*2;
        }
    }
}

Program running results

5101个村子卖出了鸭子的个数256

Topic 2: Immortal Rabbit

There is a pair of rabbits. From the third month after birth, a pair of rabbits will be born every month, and the little rabbit will give birth to a pair of rabbits every month after the third month. If the rabbits are not dead, ask the rabbits every month. What is the total?

  • Problem-solving ideas: It is relatively simple to use mathematical ideas to solve such problems. This question is about knowing how many pairs of rabbits there were at the beginning, but not knowing how many rabbits there will be later, because of the type of knowing the head but not the tail. So we need to find the law of F(n) and F(nm), m is the interval at which the data in the question changes significantly. In this question, m=1 and 2, because the birth time of each pair of rabbits is different, and the interval between growing into a big rabbit is also Not the same.
  • In order to facilitate our understanding of the data in this question, each pair of rabbits will be numbered for distinction.
    Insert picture description here
    From the figure, we can find the pattern, the logarithm of the rabbit in the third month is equal to the second month plus the first month, that is, F(3) = F(2) + F(1)
    F(4) = F (3) + F(2), sum up the law F(n) = F(n-1) + F(n-2)
    code implementation:
public class Rabbit {
    
    
    public static void main(String[] args) {
    
    
    //求第4个月的兔子对数
        int i = countRabbit(4);
        System.out.println(i);
    }
    //定义方法,数兔子
    public static int countRabbit(int month){
    
    
          if(month ==1 || month ==2){
    
    
              return 1;
          }else {
    
    
              return countRabbit(month-1) + countRabbit(month -2)  ;
          }
    }
}

Program running results

3

Guess you like

Origin blog.csdn.net/hypertext123/article/details/115267601