Several types of greed

Source: Internet
Author: User

Order:

I am not Daniel , but also a beginner , write these just to review their knowledge , of course, not to ask Daniel , everyone to my request not too high, I hope Daniel a lot of advice.

Body:

I. Algorithmic thinking: the best possible solution.

-The final solution is not guaranteed to be the optimal solution;

-cannot be used to find the maximum or minimum solution;

-The range of feasible solutions that can only be used to satisfy certain constraints.

Two. Types of questions:

(1) knapsack problem: There is a backpack, backpack capacity is m=150. There are 7 items that can be divided into any size.
It is required to maximize the total value of the items loaded into the backpack, but not to exceed the total capacity

And so on the knapsack problem many requirements detailed understanding after the topic form Summary.

     (2 ) activity scheduling problem: with n si and end time fi si,fi ,

This type of problem is sorted in ascending order of destination ( si fi) has been assigned an initial value in the ascending order of the FI), so the order is ordered before it is sorted.

#include <iostream>using namespacestd;voidGreedychoose (intLenint*s,int*f,BOOL*flag) {flag[0]=true;//*flag <=> flag[];    intj=0;  for(intI=1; i<len;i++)    {        if(s[i]>=F[j]) {Flag[i]=true; J=i; }      }  }intMain () {ints[ One]={1,3,0,5,3,5,6,8,8,2, A}; intf[ One]={4,5,6,7,8,9,Ten, One, A, -, -}; BOOLmark[ One]={0};//defining an array of marked typesGreedychoose ( One, S,f,mark);  for(intI=1;i< One; i++)        if(Mark[i]) cout<<i<<Endl; return 0;}//3 7
View Code

(3) segment Coverage problem: tells you the starting and ending coordinates of N segments (si fi)in a one-dimensional space, asking how long these segments cover

Such problems are sorted in ascending order of Origin ( si fi) has been assigned an initial value in the ascending order of Si, so the order must be ordered before it is sorted.

#include <iostream>UsingNamespaceStdIntMain () {int s[11]={2,3,4,5,6,7,8,9,10,11};int f[11]={3,5,7,6,9,8,12,10,13,15};int total=f[0]-s[0];//Attached initial valueForint i=1,j=0;i<10;i++< Span style= "color: #000000;" ) {if (S[i]>=f[i]) {total+= (F[i]-s[i]); J=i;} else {if (f[i]<=< Span style= "color: #000000;" >F[J]) continueelse {Total+=f[i]-f[j ]; J=i;}} } cout<<total<<ENDL; return 0    
View Code

(4) Number combination problem: There are N positive integers, now you need to design a program, so that they join together to become the largest number, example 3 integers ,345,678 It is obvious that the 67834512 is the largest, first of all this problem is to the number of the first value of who's big, if pure to compare in the N is very large, there will be a time-out phenomenon. We cancompare ab with A comparison (eg.a b two numbers are written in the form AB BA the size code for BA is implemented as follows:

BOOLCompareintNUM1,intnum2) {    intn1=0, n2=0; intmidnum1,midnum2;  while(MIDNUM1) {++N1; Midnum/=Ten; }     while(midnum2) {++N2; Midnum2/=Ten; }    intA=num1*pow (Ten, N2) +num2; intB=num2*pow (Ten, N1) +NUM1; return(a>b)?true: flase;}
View Code

(5) The problem of finding change: the main idea is as follows, for the value of RMB has1Yuan5YuanTenYuan -Yuan -Yuan -Yuan, the following requirements to design a program, enter the change of money, the output of the change scheme of the minimum number of programs, such as123yuan, at least1Zhang -of,1Zhang -of,3Zhang1of the yuan, altogether5Zhang!

Analysis: Such a problem using the greedy strategy is to choose the biggest money each time, if the last over, then choose the second largest face value, and so on, until the end and the money to find equal, this situation we are familiar with, the following see the main implementation code:

 for(intI=0;i<6;){    if(moneynum+moneyspecies[i]>Total )//Moneyspecies[i] represents par value;{i++; Continue; } moneynum+=Moneyspecies[i]; ++Moneyindex[i]; //Moneyindex[i] Represents the number of each face value++num; if(moneynum==Total ) Break;}
View Code

At present, can only tidy up so much, is according to their unfamiliar writing, there is nothing to learn from the place, I hope you do not take offense!

Several types of greed

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.