Recursive algorithm ideological brainwashing series (8) - The first chapter recursive thought

Original refer to the following:

Algorithm brainwashing series (8) - The first chapter recursive thought 


 

Starring learned tonight is' handed handed push ideas' '

 

1. Concept: 
  by known conditions, using specific recursive relationship gradually, until the final result obtained, the core is the constant use of existing information derived something new.

2. Category: "Forwards" and "Backward"

    Forwards: results from the introduction of conditions.

(You like to know the order of 26 letters, one can lower A B Release; already known formulas and parameters, can give the exact result)

    Backward: launch conditions from the results.

(Already know the formula and the results, to deduce the exact parameters)

 

3. 

<1> Forwards example: "Fibonacci" series, said that the problem of rabbit breeding:

If a rabbit can give birth to 1 month of bunnies, bunny and each of the first three months after birth, it can generate a small rabbit, if from 1 to newborn bunnies started 1 year can

How many rabbits breed?

Thinking: Actually, this is our rabbit can be divided into "1-month-old rabbit", "2 months old rabbit", "3-month-old rabbit."

        When the initial ①: January size rabbit pair a total of 1 pair.

        ② first month: January big bunnies into a large rabbit in February, the total number or 1 pair.

        ③ second month: February big bunnies into a large rabbit in March, a small rabbit breeding, the total number is 2 pair.

        ④ third month: 3-month-old rabbit tmd animate a small rabbit, last month in January big bunny into a large rabbit in February, a total of 3 pairs.

         ......                    ......

        F 0 = 1

        F 1 = 1

        F2=F0+F1

        F 3 = F 1 + F 2

        ......

, And F n =, and F n-the 2 +,, and F n-the 1

month = 12
fab = [1, 1]
for i in range(2, month):
    fab.append(fab[i - 2] + fab[i - 1])
for i in range(len(fab)):
    print('i:{}'.format(fab[i]))

 

Examples <2> of backstepping

       This a question of saving money, a rich second to his son four years of college save money, rich three generations can only take 3k per month as living expenses next month, using the entire sum withdrawal of ways.

Annual interest rate at 1.71% (interest generated is his pocket money 3K), wealthy people need to ask how much a one-time deposit.

Thinking: this topic is that we know the results, you need to push the reverse condition, 48 months to three generations of the rich with interest put a 3k removed, then

        47 month deposit should: (48th month deposits +3000) / (1 + 0.0171 / 12 (month));

        46 month deposit should: (47th month deposits +3000) / (1 + 0.0171 / 12 (month));

         .....                    .....

        1 month deposit should: (2nd month deposits +3000) / (1 + 0.0171 / 12 (month));

  

duration = [0] * 48
rate = 0.0171
print(rate / 12)
for i in range(len(duration) - 1, -1, -1):
    duration[i - 1] = (duration[i] + 3000) / (1 + rate / 12)
    print(duration[i - 1])
    print(i)
for i in range(len(duration) - 1, -1, -1):
    print("第{}个月末本利合计:{}".format(i + 1, round(duration[i], 2)))

 

Guess you like

Origin www.cnblogs.com/originalTblog/p/11717490.html