JavaBeispiel
<eval id="4bd6870ce3fa6"> ArrayList KanditatenListe=new ArrayList(); ArrayList ResultatenListe= new ArrayList();</eval>
int c=0; int size=0;
c = Integer.parseInt(JOptionPane.showInputDialog(null, "Bitte Kapazität eingeben:","10")); size = Integer.parseInt(JOptionPane.showInputDialog(null, "Bitte Größe der Kanditatenliste eingeben:","10"));
for(int i=0;i<size><pre> int a = Integer.parseInt(JOptionPane.showInputDialog(null, "Bitte Kanditat "+i+" eingeben:","1")); KanditatenListe.add(a); </pre><p>}</p><p>/*Kernstück des Greedy-Algorithmus*/ //=============================================================================================================== for(int index=0;index<size><pre> int a = KanditatenListe.get(index); while(a<=c){ c = c - a; ResultatenListe.add(a); }</pre><p>}</p><p>/*Prüfung ob mögliche Lösung richtig ist.*/ if(c==0){ </p><pre> /*Ausgabe des ArrayList auf der Konsole*/ System.out.print("{"); for(int i=0;i<resultatenliste.size system.out.print system.out.println><p>}else{</p><pre> System.out.println("!!!Kein Lösung gefunden!!!"); System.out.println("Rest: "+c);</pre><p>} //=============================================================================================================== </p></resultatenliste.size></pre></size></p></size>