栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PAT乙级1015 德才论 java(19/25) 测试点3、4超时 测试点2压线

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PAT乙级1015 德才论 java(19/25) 测试点3、4超时 测试点2压线

 

思路:把德分和才分存入数组中,作为Map中的value,ID作为key。自定义考生类person,将person的属性设为Map.Entry,随后重写compareTo()方法,伴随一个动态数组存储person类型的对象,最后Collections.sort()进行排序。

也是意料之中的超时了,各位如果有AC的java代码也可以分享下!

代码如下:

import java.io.*;
import java.util.*;


class person implements Comparable{
    Map.Entry info;

    public person(Map.Entry info){
        this.info=info;
    }

    //求总分方法
    public static int sum(int[] array){
        int sum=0;
        for(int temp:array){
            sum+=temp;
        }
        return sum;
    }

    @Override
    public int compareTo(Object o){
        person score=(person) o;

        int sum1=sum(this.info.getValue());
        int sum2=sum(score.info.getValue());

        int virtue1=this.info.getValue()[0];
        int virtue2=score.info.getValue()[0];

        int ID1=this.info.getKey();
        int ID2=score.info.getKey();

        //先按总分排
        if(sum1!=sum2){
            return -(sum1-sum2);
        }
        //总分并列德分排
        else if(virtue1!=virtue2){
            return -(virtue1-virtue2);
        }
        //德分并列ID排
        else{
            return (ID1-ID2);
        }
    }
}

public class Main {

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

        in.nextToken();
        int N=(int)in.nval;
        in.nextToken();
        int L=(int)in.nval;
        in.nextToken();
        int M=(int)in.nval;

        
        Map first=new HashMap<>();
        ArrayList firstList=new ArrayList<>();

        Map second=new HashMap<>();
        ArrayList secondList=new ArrayList<>();

        Map third=new HashMap<>();
        ArrayList thirdList=new ArrayList<>();

        Map forth=new HashMap<>();
        ArrayList forthList=new ArrayList<>();

        for(int i=1;i<=N;i++){
            in.nextToken();
            int ID=(int)in.nval;

            in.nextToken();
            int virtue=(int)in.nval;

            in.nextToken();
            int ability=(int)in.nval;
            int[] array={virtue,ability};

            
            if(virtue>=M&&ability>=M){
                first.put(ID,array);
            }
            else if(ability=M&&ability>=L){
                second.put(ID,array);
            }
            else if(ability>=L&&virtue>=L&&virtue>=ability){
                third.put(ID,array);
            }
            else if(ability>=L&&virtue>=L){
                forth.put(ID,array);
            }
        }

        
        for(Map.Entry info:first.entrySet()){
            person p=new person(info);
            firstList.add(p);
        }

        for(Map.Entry info:second.entrySet()){
            person p=new person(info);
            secondList.add(p);
        }

        for(Map.Entry info:third.entrySet()){
            person p=new person(info);
            thirdList.add(p);
        }

        for(Map.Entry info:forth.entrySet()){
            person p=new person(info);
            forthList.add(p);
        }

        
        Collections.sort(firstList);
        Collections.sort(secondList);
        Collections.sort(thirdList);
        Collections.sort(forthList);

        //先打录取总人数
        out.println(firstList.size()+secondList.size()+ thirdList.size()+forthList.size());

        
        for(person p:firstList){
            out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
        }

        for(person p:secondList){
            out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
        }

        for(person p:thirdList){
            out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
        }

        for(person p:forthList){
            out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
        }

        out.flush();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737474.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号