| | |
| | | static class Locker { |
| | | int id; |
| | | double remainingSpace; |
| | | long bindingTags; |
| | | Map<String, Integer> contents; |
| | | Set<String> itemTypes; // 当前储物柜中的物品种类 |
| | | |
| | |
| | | this.remainingSpace = 1.0; |
| | | this.contents = new HashMap<>(); |
| | | this.itemTypes = new HashSet<>(); |
| | | this.bindingTags = System.currentTimeMillis(); |
| | | } |
| | | |
| | | public boolean canAdd(Item item, int quantity) { |
| | |
| | | } |
| | | } |
| | | |
| | | // public static void main(String[] args) { |
| | | // Scanner scanner = new Scanner(System.in); |
| | | // |
| | | // System.out.println("请输入物品种类数量:"); |
| | | // int itemTypes = scanner.nextInt(); |
| | | // scanner.nextLine(); |
| | | // |
| | | // List<Item> items = new ArrayList<>(); |
| | | // |
| | | // for (int i = 0; i < itemTypes; i++) { |
| | | // System.out.println("\n请输入第" + (i + 1) + "种物品的信息:"); |
| | | // System.out.print("物品名称: "); |
| | | // String name = scanner.nextLine(); |
| | | // |
| | | // System.out.print("单种存放最大数量: "); |
| | | // int maxCapacity = scanner.nextInt(); |
| | | // |
| | | // System.out.print("库存数量: "); |
| | | // int stock = scanner.nextInt(); |
| | | // scanner.nextLine(); |
| | | // |
| | | // items.add(new Item(name, maxCapacity, stock)); |
| | | // } |
| | | // |
| | | // // 使用分组优先算法 |
| | | // PackingSolution solution = packItemsWithGrouping(items); |
| | | // |
| | | // System.out.println("\n" + solution); |
| | | // |
| | | // scanner.close(); |
| | | // } |
| | | public static void main(String[] args) { |
| | | int itemTypes = 5; |
| | | Scanner scanner = new Scanner(System.in); |
| | | |
| | | System.out.println("请输入物品种类数量:"); |
| | | int itemTypes = scanner.nextInt(); |
| | | scanner.nextLine(); |
| | | |
| | | List<Item> items = new ArrayList<>(); |
| | | |
| | | for (int i = 0; i < itemTypes; i++) { |
| | | String name = i+"a"; |
| | | int maxCapacity = i*10; |
| | | int stock = i*11; |
| | | System.out.println("\n请输入第" + (i + 1) + "种物品的信息:"); |
| | | System.out.print("物品名称: "); |
| | | String name = scanner.nextLine(); |
| | | |
| | | System.out.print("单种存放最大数量: "); |
| | | int maxCapacity = scanner.nextInt(); |
| | | |
| | | System.out.print("库存数量: "); |
| | | int stock = scanner.nextInt(); |
| | | scanner.nextLine(); |
| | | |
| | | items.add(new Item(name, maxCapacity, stock)); |
| | | } |
| | | |
| | | // 使用分组优先算法 |
| | | PackingSolution solution = packItemsWithGrouping(items); |
| | | for (Locker locker:solution.lockers) { |
| | | for (String mantnr : locker.contents.keySet()){ |
| | | System.out.println(mantnr+"<===>"+locker.contents.get(mantnr)); |
| | | } |
| | | } |
| | | |
| | | System.out.println("\n" + solution); |
| | | |
| | | // scanner.close(); |
| | | scanner.close(); |
| | | } |
| | | // public static void main(String[] args) { |
| | | // int itemTypes = 5; |
| | | // |
| | | // List<Item> items = new ArrayList<>(); |
| | | // |
| | | // for (int i = 0; i < itemTypes; i++) { |
| | | // String name = i+"a"; |
| | | // int maxCapacity = i*10; |
| | | // int stock = i*11; |
| | | // items.add(new Item(name, maxCapacity, stock)); |
| | | // } |
| | | // |
| | | // // 使用分组优先算法 |
| | | // PackingSolution solution = packItemsWithGrouping(items); |
| | | // for (Locker locker:solution.lockers) { |
| | | // for (String mantnr : locker.contents.keySet()){ |
| | | // System.out.println(mantnr+"<===>"+locker.contents.get(mantnr)); |
| | | // } |
| | | // } |
| | | // System.out.println("\n" + solution); |
| | | // |
| | | //// scanner.close(); |
| | | // } |
| | | |
| | | /** |
| | | * 分组优先装箱算法 |