This article focuses on some basic algorithms, which are the cornerstone of many other algorithms.
It mainly includes simulation, violence, enumeration, recursion, greed, and division .
1, Analog:
As the name implies, simulation is ... Hungry, simulation, there are some topics that give you some steps, and then you write the code step-by-step to simulate those steps on the line. This kind of topic mainly belongs to the basic problem, but of course if need to simulate the step enough disgusting, still more troublesome ...
Examples of specific simulations will be encountered in the following exercises, according to the requirements of the topic step-by-step, the general algorithm is less difficult.
2, Violence:
As the name implies, violence is ... Hungry, violent. For example, the topic lets the 10 points from the plane take three points, so that they form the largest triangle area. Then the method of violence is to directly enumerate all possible combinations, and then one by one, to find the maximum value inside ... It's so violent.
Of course, violence is not necessarily a brain-free brute force, and sometimes it may be necessary to rule out some impossible situations to make the violence faster.
Of course, the use of violence to see the topic of the data range, of course, also have the courage, a word is how bold people, the land of how big production ...
3, enumeration:
As the name implies, enumerations are ... Hungry, enumeration. is to enumerate all the numbers, and then one by one to calculate the better. And the feeling of violence is actually similar.
For example, the second problem of the H, you need a more intelligent enumeration. Enumerating each of the starting points will obviously time out, so you need a variety of conversions to become an enumeration length, and then you can prove that you only need to enumerate the square root n times on the line.
So many topics require some clever conversion, and then enumerate ...
4, recursion:
As the name implies, recursion is ... Hungry, recursive. C + + book above should have said a function of recursion, did not see first go back to see.
And then there is the classic question of the Nottingham on recursion.
Specific Hanoi is what please Baidu or Google.
Then solve the problem now: an N-layer Hanoi, outputting his moving steps, from a to C-bar.
The solution is that we first move a N-1 Hanoi from A to B, and then move the last one from A to C, and then move the N-1 Hanoi from B to C on the line ...
N-1 layer of the Hanoi recursive solution on the line ...
The specific code is as follows:
voidHanoi (intNCharACharBCharC) { if(n==1) {cout<<A<<" -"<<C<<Endl; return; } Hanoi (N-1, a,c,b); N-1, from A to B. cout<<A<<" -"<<C<<Endl; Hanoi (N-1, B,a,c); N-1, from B to C. }
Suggest a good look at the best understanding of this code ...
Recursion is almost like this, with the help of the recursive function, the big problem into a small problem to solve, and then merge the small problem ...
5, Greedy:
As the name implies, greed is ... Hungry, greedy. There are some problems that make the final result optimal, and then we can directly traverse, each time for the current situation to take the best, ignore will not affect other circumstances, and as long as can prove each time to take the best, the total result is the best, then this greedy is correct ...
For example, the following questions:
You take a backpack with a capacity of V to go to the market, there are n kinds of moral integrity in the market, where the price of the unit volume of the moral integrity is Pi, then the maximum Ci volume of moral integrity sold.
Then ask at least how much money can fill the backpack full moral integrity?
The typical greedy topic, obviously buy the cheapest best, so every time to find pi the smallest one to buy, if all buy also pack dissatisfaction backpack, buy pi second small ... And then, all the way up to full backpack ...
So every time to take the cheapest to buy, then the last to spend the least money, so the correctness of needless to say it ...
Greed is almost like this, but note that some problems may seem right, but may be greedy and loopholes, it may seem to be not right, but in fact, can prove that some kind of greed is right ...
6, Divide and conquer:
As the name implies, divided treatment is ... Be hungry, divide and conquer. Divide and conquer, which is similar to recursion, in fact, divide and conquer is generally achieved through the return of ...
Divide the big problems into small problems, and then merge the small problems into the solution of big problems. Specific examples of the words will later say, are very classic algorithm ...
Concrete basic algorithm In fact, these are the few, they are very basic things, the proposal can be preliminary grasp first. Then a lot of algorithms are built by these algorithms ...
The basic algorithm of algorithm recording