Greedy algorithm for Change (Java)

Source: Internet
Author: User

greedy algorithm thought

Greedy algorithms always make choices that seem to be good at the moment. That is, the greedy algorithm is not considered from the overall end, he made the choice is only the local best choice. What he has done is only a partial optimal solution in a sense. The greedy algorithm can not get the whole optimal solution for all the problems, but it can produce the whole optimal solution or the approximate solution of the whole optimal solution for many problems with wide range.

1. Algorithmic thinking

greedy algorithm is a kind of method that does not pursue the optimal solution and only hopes to get a satisfactory solution. Greedy algorithm can be quickly satisfied with the solution, because it saves to find the best elder sister to exhaust all Ken and must spend a lot of time. greedy (heart) algorithm is an improved hierarchical processing method. Its core is to choose a measure standard according to test instructions. These inputs are then ranked in the order required by this metric, and a quantity is entered one at a time in this order. This input is not added to this decomposition if the input is combined with some of the best solutions that are currently present in this metric sense and cannot produce a workable solution. This kind of hierarchical processing method, which can get the optimal solution under some measure, is called greedy algorithm.

There are often several metrics that can be measured for a given problem. At first glance, these metrics seem to be desirable, but in practice, most of the metrics used for greedy processing are not the optimal solution to the problem, but the suboptimal solution. Therefore, the selection of optimal measurement criteria for the optimal solution of the problem is the core of using greedy algorithm.

In general, it is not easy to choose the best metric, but it is particularly effective to solve a problem with greedy algorithm after choosing the best measure standard. The optimal solution can be achieved by a series of local optimal selection, greedy selection, according to the current state to make the best choice at present, that is, local optimal solution selection, and then to solve the corresponding sub-problems resulting from the selection. Every time we make a greedy choice, the problem is reduced to a smaller sub-problem, and finally a whole optimal solution of the problem can be obtained.

2. Basic Features

From one of the initial release of the problem, the target is gradually approached to obtain a better solution as soon as possible. When a step in the algorithm can no longer move forward, the algorithm is stopped and the approximate solution is given.

3. Example: Change

This program realizes supermarket cashier's change plan, enters the amount which needs to any coins to the customer, by the procedure calculates that the amount may have the face value renminbi composition. RMB hypothesis 100 50 20 10 5 2 1 0.5 0.2 0.1

package exercises; Import Java.util.arraylist;import Java.util.list;import Java.util.scanner;public class greedy algorithm find change {public static int max=10;public static double value[]={10000,5000,2000,1000,500 , 200,100,50,20,10};//public static Double value[]={100,50,20,10,5,2,1,0.5,0.2,0.1}; If you change to this line, the result will be error public static int Num[]=new Int[max];p ublic static void Main (string[] args) {List ag=new ArrayList (); for (int i=0;i<value.length;i++) { Ag.add (Value[i]);} System.out.println ("Please enter a value to be changed"); Scanner scanner=new Scanner (system.in);d ouble a=scanner.nextdouble (); Conver (a*100); System.out.println ("Change"), for (int i=0;i<max;i++) {if (num[i]>0) {System.out.println ("face value" +value[i]/100+ "total required" + Num[i]+ "Zhang");}}}  private static void Conver (double a) {//TODO auto-generated method Stubint i,j;for (i=0;i<max;i++) if (A>value[i]) Break;while (A>0&&i<max) {if (A>=value[i]) {a-=value[i];num[i]++;} else if (a<10&&a>=5) {num[max-1]++;break;} elsei++;}}} 

Results:

Please enter a value to be replaced
212.1
Change
Face value 100.0 altogether requires 2 sheets
Face value 10.0 altogether requires 1 sheets
Face value 2.0 altogether requires 1 sheets
Face value 0.1 altogether requires 1 sheets

Greedy algorithm for Change (Java)

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.