--java implementation of entry-level greedy algorithm

Source: Internet
Author: User

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

Related Article

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.