Data Structure and algorithm greedy algorithm C ++ implementation, data structure and algorithm greedy

Source: Internet
Author: User

Data Structure and algorithm greedy algorithm C ++ implementation, data structure and algorithm greedy
1. Basic Idea: gradually approach a given goal from the trigger of an initial solution of the problem, and obtain a better solution as quickly as possible. When one step of the algorithm is reached, the algorithm is stopped and an approximate value is given. That is to say, the greedy algorithm does not take the overall optimization into consideration. All it makes is a local optimal choice in a certain sense. Existing problems: 1. The final solution cannot be guaranteed to be optimal; 2. The problem cannot be used to obtain the maximum or least solution; 3. The problem can only be used to obtain the range of feasible solutions that meet certain constraints. Implementation process: Starting from an initial solution of the problem; while (one step forward toward the given overall goal) {use feasible decisions to find a solution element of the feasible solution ;} combine all solution elements into a feasible solution to the problem. Basic Elements of greedy algorithms: (How do you know whether to use greedy algorithms to solve a specific problem?) (1) and greedy choice.Greedy choiceIt refers to the questionOverall Optimal SolutionYou can use a seriesLocal optimumIn other words, when considering the selection, we only consider the best choice for the current problem rather than the results of the sub-problem. This is the first basic element for the feasibility of greedy algorithms. For a specific problem, to determine whether it has the greedy choice nature, it must prove that the greedy choice made by each step ultimately leads to the overall optimal solution of the problem. (2) optimal sub-structure. When the optimal solution of a problem includes the optimal solution of its subproblemsOptimal sub-structure. The optimal sub-structure of a problem is the key feature of the problem that can be solved by greedy algorithms. Example: change the currency of RMB 100, 50, 20, 10, 5, 1, 0.5, 0.2, and 01. Combine a certain amount of money with a non-denomination. Code:

# Include <iostream> const int SIZE = 9; int coin [SIZE] = {10000,500 0, 2000,100 0, 500,100, 50, 20, 10}; int num [SIZE]; int exchange (int n); int main () {using namespace std; double money; string name [SIZE] = {"one hundred yuan", "50 yuan ", "20 yuan", "10 yuan", "5 yuan", "1 yuan", "5 cents", "2 cents", "1 cent "}; cout <"Enter the amount:"; cin> money; int n = (int) (money * 100); exchange (n); for (int I = 0; I <SIZE; I ++) {cout <name [I] <"currency:" <num [I] <"Zhang \ n ";} return 0;} int exchange (int n) {int I; for (I = 0; I <SIZE; I ++) if (n> = coin [I]) break; while (n> 0 & I <SIZE) {if (n> = coin [I]) {n-= coin [I]; num [I] ++ ;} else if (n <10 & n> = 5) {num [SIZE-1] ++; break;} else I ++;} return 0 ;}
Running result:

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.