前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >拼多多笔试

拼多多笔试

作者头像
名字是乱打的
发布2022-05-13 09:04:57
3060
发布2022-05-13 09:04:57
举报
文章被收录于专栏:软件工程

忘了题目是啥了,不准复制题目,过两天,题目出来再补上题目...

代码语言:javascript
复制
package com.algorithm.offer;


import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int k=sc.nextInt();
        if (k<=0||k>n||k>1000||n<=0||k>1000){
            System.out.println("error");
            return;
        }

        int[] low=new int[n];
        int[] high=new int[n];
        int i=0;
        PriorityQueue<Integer> minQueue=new PriorityQueue<>();
        PriorityQueue<Integer> maxQueue=new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        
        while (i<n){
            low[i]=sc.nextInt();
            high[i]=sc.nextInt();
            minQueue.add(low[i]);
            maxQueue.add(high[i]);
            if (Math.abs(low[i])>50||Math.abs(high[i])>50){
                System.out.println("error");
                return;
            }
            i++;
        }
        int aloMin=100;
        boolean overmin=false;
        while (!minQueue.isEmpty()){
            int OKmin=0;
            aloMin=minQueue.poll();
            for(int j=0;j<n;j++){
                if (aloMin>=low[j]){
                    OKmin++;
                }
                if (OKmin==k){
                    overmin=true;
                    break;
                }
            }
            if (overmin==true){
                break;
            }
        }

        int aloMax=100;
        boolean overMax=false;
        while (!minQueue.isEmpty()){
            int OkMax=0;
            aloMax=maxQueue.poll();
            for(int j=0;j<n;j++){
                if (aloMax<=high[j]){
                    OkMax++;
                }
                if (OkMax==k){
                    overMax=true;
                    break;
                }
            }
            if (overMax==true){
                break;
            }
        }
        if ((aloMin!=100&&overmin!=false)&&(aloMax!=100&&overMax!=false)){
            System.out.println(aloMin+" "+aloMax);
            return;
        }
        System.out.println("error");
        return;
    }
}
代码语言:javascript
复制
package com.algorithm.offer;

import java.util.*;

public class Main2 {
   static class baozi{
        int index;
        int xiandu;
        int down;

        public baozi(){
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public int getXiandu() {
            return xiandu;
        }

        public void setXiandu(int xiandu) {
            this.xiandu = xiandu;
        }

        public int getDown() {
            return down;
        }

        public void setDown(int down) {
            this.down = down;
        }
    }
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        PriorityQueue<baozi> xianQueue=new PriorityQueue<>(new Comparator<baozi>() {
            @Override
            public int compare(baozi o1, baozi o2) {
                if (o2.xiandu-o1.xiandu==0){
                    return o2.down-o1.down;
                }
                return o2.xiandu-o1.xiandu;
            }
        });
        PriorityQueue<baozi> temp=new PriorityQueue<>(new Comparator<baozi>() {
            @Override
            public int compare(baozi o1, baozi o2) {
                if (o2.xiandu-o1.xiandu==0){
                    return o2.down-o1.down;
                }
                return o2.xiandu-o1.xiandu;
            }
        });
        //包子进来
        int i=0;
        while (i<n){
            baozi baozi=new baozi();
            baozi.setXiandu(sc.nextInt());
            baozi.setDown(sc.nextInt());
            baozi.setIndex(i);
            xianQueue.add(baozi);
            i++;
        }
        //吃包子 (当天也要吃)
        int day=0;
        int canEat=n;//能吃的包子初始值为n;
        while (day<=m){
            int count=0;
            boolean preOver=false;//是否提前结束
            //每天先吃两个
            while (count!=2){
                count++;
                baozi baozi = xianQueue.poll();
                if (baozi.getXiandu()>0){
                    baozi.setXiandu(-1);
                    canEat--;
                    temp.add(baozi);
                }else {
                    temp.add(baozi);
                    //如果每天吃的包子都是过期的或者已经吃过的就没必要过下面的天了.
                    preOver=true;
                    break;
                }
            }
            if(preOver==true||canEat<m){ //若提前结束,直接转移包子,和交给哆啦A梦检查.
                while (!xianQueue.isEmpty())
                {
                    temp.add(xianQueue.poll());
                }
                cheack(temp,canEat,m);
                break;
            }

            //吃剩下的包子要下降鲜度
            while (!xianQueue.isEmpty())
            {
                baozi baozi=xianQueue.poll();
                if (baozi.getXiandu()>0){ //如果没被吃或者没过期
                    int xiandu=baozi.getXiandu()-baozi.getDown()>0?baozi.getXiandu()-baozi.getDown():0;
                    baozi.setXiandu(xiandu);
                }
                if (baozi.getXiandu()<=0){
                    canEat--;
                }
                temp.add(baozi);
            }
            PriorityQueue<baozi> dd=new PriorityQueue<>(new Comparator<baozi>() {
                @Override
                public int compare(baozi o1, baozi o2) {
                    if (o2.xiandu-o1.xiandu==0){
                        return o2.down-o1.down;
                    }
                    return o2.xiandu-o1.xiandu;
                }
            });
            xianQueue=temp;
            temp=dd;

            day++;
        }
    }

    private static void cheack(PriorityQueue<baozi> xianQueue,int canEat,int m) {
       //首先判断是否发怒
        if (canEat<m){
       //创建一个以序号排列的队列,并显示
            PriorityQueue<baozi> pringQueue=new PriorityQueue<>(new Comparator<baozi>() {
                @Override
                public int compare(baozi o1, baozi o2) {
                    return o1.getIndex()-o2.getIndex();
                }
            });

            //装包子
            while (!xianQueue.isEmpty())
            {
                pringQueue.add(xianQueue.poll());
            }
            //打印
            while (!pringQueue.isEmpty())
            {
                System.out.println(pringQueue.poll().xiandu);
            }
        }
        return;
    }
}

4道题目,只写出2.6道编程,不好意思

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档