=========================================================================
=========================================================================
public class pan { static ArrayList <Integer> children =new ArrayList<>(); static ArrayList <Integer> pies =new ArrayList<>(); static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { while (true){ System.out.println("请输入饼干序列:"); System.out.println("输入0退出。"); int choise =scanner.nextInt(); if (choise==0){ break; } pies.add(choise); } while (true){ System.out.println("请输入小孩儿序列:"); System.out.println("输入0退出。"); int choise =scanner.nextInt(); if (choise==0){ break; } children.add(choise); } pies.sort(null); children.sort(null); System.out.println(pies); System.out.println(children); //遍历饼干和小孩儿列表,若饼干>=小孩儿,同时后移,否则饼干后移,直到饼干吃完或者小孩不满足 int pies_index=0; int children_index=0; while (true){ if (pies_index>pies.size()-1||children_index>children.size()-1)break; if (pies.get(pies_index) >= children.get(children_index)){ pies_index+=1; children_index+=1; } else if (pies.get(pies_index) < children.get(children_index)) { pies_index+=1; } } if (children_index>children.size()-1){ System.out.println("分配完成,让"+children.size()+"名孩子满足!"); } else if (pies_index>=pies.size()-1&&children_index<=children.size()-1) { System.out.println("分配失败,但让"+(children_index)+"名孩子满足!"); } } }
=======================================================================================