Getting Started with greedy algorithms
Greedy algorithm is a kind of idea, not a formula.
Look seriously, it will be a while!
Personal website: multi-cat Movie (can see each big VIP video) www.duomao.xyz
Packagecom.niu.test;ImportJava.util.Scanner;/*** Created by Administrator on 2017/9/28.*/ Public classTanXin1 {/*** Greedy algorithm, looking for change * Suppose there is 100 50 10 10 money for the changes, the money to return the changes and the number of dollars * * Ideas: * while (can be a step forward for a given target) {* Using feasible decisions, to find out An element of the row solution *} * A workable solution that is problematic by the combination of all elements; * Here I don't have a combination of the results of the withdrawal, directly printed output. * Circular thinking: * 1, starting from 100 yuan change to 1 yuan: while (I <= (a.length-1)) * 2, judge the current change money is greater than the rest of the money to change (otherwise change is negative): Count >= a[i], if less than C Ount to the next denomination of the currency * 3, output change of money and number: System.out.println ("Change:" + a[i] + "Yuan" + count/a[i] + "Zhang. "); * 4, if the remaining money is 0, change the end otherwise i+1 to the next currency value * *@paramargs*/ Public Static voidMain (string[] args) { while(true) { int[] A = {100, 50, 10, 5, 1}; System.out.println ("Enter the money for change:"); Scanner SC=NewScanner (system.in); //Count Total Amount intCount =Sc.nextint (); //I total currency type inti = 0; while(I <= (a.length-1)) { //Do not look for change more than A[i]. if(Count >=A[i]) {System.out.println ("Change:" + a[i] + "Yuan" + count/a[i] + "Zhang. "); //the remaining count equals count minus the amount that has been change. Count = count-count/a[i] *A[i]; if(count = = 0) {System.out.println ("The change is complete!" "); Break; } Else{i++; } } Else{i++; } } } }}
Results:
--java implementation of entry-level greedy algorithm