8.16扣...

=========================================================================

=========================================================================

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)+"名孩子满足!");
        }
    }
}
=======================================================================================

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值