Chapter IV algorithm

First, the understanding of the greedy algorithm

  Understand, greedy algorithm is similar mathematical induction ideas, expanding from the local to the whole, that is, from finding the best local choice, the same has been so greedy strategy choice to go, and finally a whole would be the optimal solution. For example, the change as currency issue, the need to find a minimum requirement of paper money, the greedy strategy is based here come from largest to smallest denomination zero, the first step is to find the largest selection greedy denomination, this is the first the optimal solution step; the second step greedy choose to find the second largest denomination, and so on, each step is the current optimal solution, and finally as a whole must be the optimal solution. But not all problems can be successfully find the corresponding greedy strategy, so trade-offs when using a greedy algorithm when to use dynamic programming. In addition, the greedy algorithm to find the most important thing is greedy strategy, we must try to counter example, to find the greedy strategy is a top priority.

 

Second, select the greedy nature of vehicle refueling problem

  First, you can fill up with n kilometers, will directly determine the distance of each of the two stations can not exceed n, otherwise it is not next to a gas station on the output "No solusion!". As for how to calculate the minimum number of refueling, my algorithm is to calculate the total in the first station to fill up after each number is divided by the number of kilometers traveled may be n the last station, we arrive at a total number of refueling.

 

Third, the problems encountered in the study and knot the case of programming

1, problems encountered in the study

  We may soon see a problem but did not think of greedy strategy how to think about programming directly counter-examples, and may lead to later discover the error greedy strategy needs to be rethought and re-programming greedy strategy needs more time. Also we need to try if there are different greedy strategies to solve a problem, many thinking the next.

2, the case where pair programming

  The Yu and Lin Shangjishijian, the first question we will soon think a greedy strategy and implementation. But the second question time, because we still believe that soon came up with the right strategy greedy, then bug has been encountered in that change, also found on the topic it wrong, once again successfully debug went on to meet new bug. That has been thought greedy strategy is wrong, always feel that we can continue this success debug. Finally found under the guidance of the teacher's choice is greedy strategy is wrong, but had no time. That thinking greedy policy of caution by rigorous, it can not come up with a reckless to try, you should think about the correctness of the greedy strategy in the face of bug, can not hang in a tree. In general, the practice of one another are a bit lacking, need more practice.

Guess you like

Origin www.cnblogs.com/ErwinG/p/11912132.html