Initial knowledge algorithm

Source: Internet
Author: User

Algorithm one: Recursion

One: Concept

The core is to use the existing information to deduce the new things by using the known condition, gradually recursion by the specific relation and finally getting the result.

II: Classification

Of course, there are two kinds of recursion, "push forward" and "reverse push"

Push: Results from the condition.

Inverse push: The conditions are introduced from the results.

Three: examples

<1> examples of shun-push

The university should know the famous "Fibonacci" series, that is, the problem of breeding rabbits, the topic I will probably say.

If 1 pairs of rabbits can have 1 pairs of rabbits a month, and each pair of rabbits in its 3rd month after birth can have 1 pairs of rabbits, if starting from 1 pairs of newborn rabbits, 1 years later can

How many rabbits are bred?

Idea: Actually this question we can divide the rabbit into "January big Rabbit", "February Big Rabbit", "March Big Rabbit".

① initial: A pair of January-size rabbits with a total of 1 pairs.

② First Month: January The Big Rabbit became the February Big Rabbit, the total is still 1 pairs.

③ Second Month: February The Big Rabbit became the March Big Rabbit, bred a pair of small rabbits, the total is 2 pairs.

④ Third Month: March the big Rabbit TMD had a pair of rabbits, last month the Big Rabbit became February Big Rabbit, the total is 3 pairs.

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

F0=1

F1=1

F2=f0+f1

F3=f1+f2

......

Fn=fn-2+fn-1

You see, is not the embodiment of the "recursive" core idea, the code is very simple.

1             int month = 12;

3 int[] fab = new Int[month];

5 fab[0] = 1;

7 Fab[1] = 1;

9 //Launch results from known conditions
Ten for (int i = 2; i < month; i++)
Each {
Fab[i] = fab[i-1] + fab[i-2];
- }

(int i = 0; i < month; i++)
+ {
Console.WriteLine ("{0} months Rabbit: {1}", I, fab[i]);
(+ }

<2> examples of Inverse push

This is a question about saving money, a rich second generation to his son's four years of college life to save a sum, rich generation can only take 3k per month as the next month's living expenses, the use of the whole deposit 0 to take the way,

Annual interest rate at 1.71%, ask rich second generation need to deposit how much money.

Train of thought: This topic is we know the result, need to reverse push condition, 48th month rich three generations to even this belt to take the 3k a take away, then

The 47th month Deposit shall be: (Deposit of the 48th month +3000)/(1+0.0171/12 (month));

The 46th month deposit shall be: (Deposit of the 47th month +3000)/(1+0.0171/12 (month));

.....                    .....

The 1th month deposit shall be: (Deposit of the 2nd month +3000)/(1+0.0171/12 (month));

1   //Bank Withdraw Money question
2 double[] month = new double[49];

4///The last one months of the linked interest is 3000
5 month[48] = 3000;

7 Double rate = 0.0171;

9 //Reverse push
Ten for (int i = i--; i > 0;)
Each {
Month[i] = (month[i + 1] + month[48])/(1 + RATE/12);
- }

(int i = i--; i > 0;)
+ {
Console.WriteLine ("{0} month end Benli total: {1}", I, month[i]);
(+ }

Initial knowledge algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.