25.22 (Bin packing with largest object first) Rewrite the preceding program that places an object with the largest weight into the first bin in which it would fit. Give an example to show that this program does not produce an optimal solution.
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Exercise22 { public static void main(String[] args) { @SuppressWarnings("resource") Scanner input = new Scanner(System.in); System.out.print("Enter the number of objects: "); int numberOfObjects = input.nextInt(); ArrayList<Integer> objects = new ArrayList<>(); System.out.print("Enter the weights of the objects: "); for (int i = 0; i < numberOfObjects; i++) { objects.add(input.nextInt()); } int container = 1; while(!objects.isEmpty()) { System.out.println("Container " + container++ + " contains objects with weight " + getConteiner(objects, 10)); } } static String getConteiner(ArrayList<Integer> objects, int max) { if(objects.isEmpty()) { return ""; } int min = Collections.min(objects); if(min <= max) { String result = ""; for (int i = 0; i < objects.size(); i++) { if((objects.get(i) > min) && (objects.get(i) <= max)) { min = objects.get(i); } } result += min + " "; objects.remove(new Integer(min)); return result + getConteiner(objects, max - min); } else { return ""; } } }
No comments :
Post a Comment