Lecture 63: Monkeys eat peaches in Python programming cases

1. Requirements description and analysis

Description of Requirement:

The monkey picked a few peaches on the first day, and ate half of the peaches on the first day. He didn't feel satisfied, so he ate one more.

The next morning, I ate half of the remaining peaches from the first day, and I didn't feel satisfied, so I ate another one.

On the third morning, I said that I ate half of the remaining peaches the next day, and I didn't feel satisfied, so I ate another one.

From then on, I will eat half of the remaining peaches from the previous day every morning, and I will eat one more.

On the 10th day, I found that there was only one peach left. How many peaches did the monkey pick on the first day?

demand analysis:

The monkey will eat half of the number of peaches left in the previous day every day, and the following reasoning process can be obtained:

  • On the first day, he ate half of the peaches, and there was half left, and he ate one more. On the second day, he ate half of the peaches left on the first day, and also ate one more.
  • Then you get: the number of peaches on the first day is twice the number of peaches eaten on the second day plus 1, and the number of peaches on the second day is 2 times the number of peaches eaten on the third day plus 1 times, and so on, until only 1 peach remains. The number of peaches on day n is twice the number of peaches eaten on day n+1 plus 1.

When calculating the number of peaches a monkey eats every day, it needs to be counted from the 10th day, because it is clear that there is only one peach on the 10th day, and then count the number of peaches on the 9th day, the peach tree on the 9th day is equal to the 10th day The number of peach trees plus 1 times 2, the peach tree on the 9th day is (1+1)*2=4, and the 9th day is 4 peaches. According to this idea, the 8th day and the 7th day have been calculated. Until the first day, add up the number of peaches every day, that is how many peaches were picked on the first day.

2. The recursive method implements the program

First, use the recursive method to implement this program. The design idea is as follows:

  • It is known that the number of peaches on the first day is twice the number of peaches eaten on the second day plus 1, and the number of peaches on the second day is twice the number of peaches eaten on the third day.

  • We can put the number of peaches per day mpin a list, fill the list with 11 None values, there are only 10 days, and the purpose of filling 11 elements is that the element with index 0 will not be processed, so fill 11 elements , it is known that the number of peaches on the 10th day is 1, then initialize the element with index 10 to 1: mp[10]=1.

  • nThe number of peaches in the day can be set to be mp[n], and the recursive expression is: mp[n]=(mp[n+1]+1)*2, and mp[10]=1.

  • After the expression is determined, we can write a for loop. It is known that the 10th day is a peach, then the cycle starts from the 9th day, and the recursive cycle is known to the 1st day. Every time the cycle is performed, according to Recursive expression, calculate the peach tree of this day, the recursive expression will add 1 to the number of peaches in the next day and multiply it by 2, so there is no need to manually add up the number of peaches per day, and it must be accurate on the first day of peaches.

    For example, on the 9th day in the first cycle, according to the recursive expression mp[9] = (mp[9+1] + 1) *2= mp[9] = (mp[10] + 1) = 2 * 2 = 4 , the number of peaches on the 9th day is 4, and so on.

def monkey_peach():
    #首先定义一个列表,存放11个元素,表示猴子每天的桃子数量,放11个是因为索引为0的元素不会被处理,10天,一直要处理到索引为10的元素,因此要放11个元素
    mp = [None] * 11
    #已知第10天的桃子数量为1,因此直接给第10天赋值为1,表示1个桃子
    mp[10] = 1
    #第10天不用处理,从第9天开始循环遍历,一直到第1天,需要采用逆推的方式,因此步长为-1
    for n in range(9, 0, -1):
        #带着每次循环的天数,去套逆推表达式,得到本天的桃子数量
        mp[n] = (mp[n + 1] + 1) * 2
    #由于当天的桃子数量都是后一天桃子数+1的2倍,因此无需对列表中的元素进行累加,求第一天摘了多少个桃子,直接返回索引为1的元素即可
    return mp[1]

print('猴子第一天一共摘了{}个桃子'.format(monkey_peach()))

image-20220831170944021

3. Implement the program recursively

Using the recursive method, each time the current day + 1 will be used to get the peach tree of the next day, and then add 1 and 2 to get the peach tree of the current day. This recursive relationship is equivalent to calling the function again inside a function. So we can use a recursive function to implement this example.

The end condition of the recursion is to return 1 when the loop reaches the 10th day.

code show as below:

def monkey_peach(day):
    if day == 10:
        return 1
    else:
        return (monkey_peach(day + 1) + 1) * 2

print('猴子第一天一共摘了{}个桃子'.format(monkey_peach(1)))

image-20220831170944021

The recursive cycle process is: bring the passed parameters into the loop body, start the recursion until the loop end condition is met, the recursion ends, and then start to return, the result of the last recursive execution is forwarded to the previous recursive Perform operations until the first recursive result, and finally return the corresponding result.

The recursive traversal process of the monkey eating peaches:

1) First start recursion

recursive function:monkey_peach(day + 1) + 1)

  • When the function is called, the actual parameter passed in day is 1, that is, day=1, which is regarded as day1.
  • Start the first recursion: day+1=1+1=2, that is, day1+1, which is regarded as day2.
  • Start the second recursion: day+1=2+1=3, that is, day2+1, which is regarded as day3.
  • Start the third recursion: day+1=3+1=4, that is, day3+1, which is regarded as day4.
  • Start the fourth recursion: day+1=4+1=5, that is, day4+1, which is regarded as day5.
  • Start the fifth recursion: day+1=5+1=6, that is, day5+1, which is regarded as day6.
  • Start the sixth recursion: day+1=6+1=7, that is, day6+1, which is regarded as day7.
  • Start the seventh recursion: day+1=7+1=8, that is, day7+1, which is regarded as day8.
  • Start the eighth recursion: day+1=8+1=9, that is, day8+1, which is regarded as day9.
  • Start the ninth recursion: day+1=9+1=10, 10 satisfies the recursion end condition: if day == 10, the recursion ends at this time, and the ninth recursion gets the return return value 1.

2) Then start the regression traversal calculation with the return value

  • Return with the return value of the ninth recursion.

  • Return to the eighth recursion, that is, day9. At this time, the result of day9 is that ((day9+1)+1)*2day9+1 is day10, and the value of day10 is 1, and then put into the formula, ie day9=(1+1)*2=4.

  • Return to the seventh recursion, that is, day8. At this time, the result of day8 is that ((day8+1)+1)*2day8+1 is day9, and the value of day9 is 4, and then put it into the formula, that isday8=(4+1)*2=10

  • Return to the sixth recursion, that is, day7. At this time ((day7+1)+1)*2, the result of day7 is that day7+1 is day8, and the value of day8 is 10, and then put it into the formula, that isday7=(10+1)*2=22

  • Return to the fifth recursion, that is, day6. At this time, the result of day6 is that ((day6+1)+1)*2day6+1 is day7, and the value of day7 is 22, and then put it into the formula, ie day6=(22+1)*2=46.

  • Return to the fourth recursion, that is, day5. At this time ((day5+1)+1)*2, the result of day5 is that day5+1 is day6, and the value of day6 is 46, and then put it into the formula, that isday5=(46+1)*2=94

  • Return to the third recursion, that is, day4. At this time, the result of day4 is that ((day4+1)+1)*2day4+1 is day5, and the value of day5 is 94, and then put it into the formula, that isday4=(94+1)*2=190

  • Return to the second recursion, that is, day3. At this time, the result of day3 is that ((day3+1)+1)*2day3+1 is day4, and the value of day4 is 190, and then put it into the formula, that is day3=(190+1)*2=382.

  • Go back to the first recursion, that is, day2. At this time, the result of day2 is that ((day2+1)+1)*2day2+1 is day3, and the value of day3 is 382, ​​and then put it into the formula, that isday2=(382+1)*2=766

  • At this point, the recursion and regression are over, and the return value 766 is used to calculate the day1 passed by the calling function. The result of day1 is ((day1+1)+1)*2day1+1 day2, which is the operation result of the recursive function 766, which is inserted into the formula.day1=(766+1)*2=1534

1534 is the return value of the function.

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/131695138