lyh20021209 2022-02-05 19:03 采纳率: 76.9%
浏览 96
已结题

一道PAT1100 校庆 运行超时

img

img

我的思路是 先读取所有数据,遍历两个数组,然后创建数组用来存储实际到场的校友的ID,然后根据校友到场人数查询相应数组,遍历数组输出年龄最大的人
但是自己写的时候也意识到暴力很容易超时,但还是没能想出来优化的方法,最终两个测试点超时
代码如下:


import java.io.*;

public class Main{

    //找年龄最大的方法
    public static String FindOldest(String[] list){
        int max=99999999;
        String maxID="";
        for(String info:list) {
            if (info != null) {
                int date = Integer.parseInt(info.substring(6, 14));
                if (date < max) {
                    max = date;
                    maxID = info;
                }
            }
        }
        return maxID;
    }

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

        //校友数组
        int N=Integer.parseInt(br.readLine());
        String[] alumni=new String[N];
        for(int i=0;i<N;i++){
            alumni[i]=br.readLine();
        }

        //来宾数组
        int M=Integer.parseInt(br.readLine());
        String[] guest=new String[M];
        for(int i=0;i<M;i++){
            guest[i]=br.readLine();
        }

        //实到校友计数器和数组
        int count=0;
        String[] alumniGuest=new String[N];

        //遍历来宾数组和校友数组 发现同ID存入实到数组
        for(int i=0;i<M;i++){
            for(int j=0;j<N;j++){
                if(guest[i].equals(alumni[j])){
                    count++;
                    alumniGuest[j]=alumni[j];
                    break;
                }
            }
        }

        //最终输出
        String Oldest="";

        //题目要求
        if(count!=0){
            Oldest=FindOldest(alumniGuest);
        }
        if(count==0){
            Oldest=FindOldest(guest);
        }

        System.out.println(count);
        System.out.println(Oldest);

    }
}

img


最终测试点三四超时,请问具体怎么解决?

  • 写回答

2条回答 默认 最新

  • qq_37950685 2022-02-07 22:06
    关注

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

    public class Demo {
    //找年龄最大的ID
    private static String getMaxID(Map<String,Integer> map){
    int max=Integer.MAX_VALUE;
    String maxID="";
    Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
    while (iterator.hasNext()){
    Map.Entry<String, Integer> next = iterator.next();
    if(max>next.getValue()){
    max = next.getValue();
    maxID = next.getKey();
    }
    }
    return maxID;
    }

    public static void main(String[] args) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(inputStreamReader);
        String key="";
        Map<String,Integer> alumni = new HashMap<>();//校友集合
        Map<String,Integer> guest = new HashMap<>();//来宾集合
        Map<String,Integer> alumniGuest = new HashMap<>();////实到校友集合
    
        int N=Integer.parseInt(br.readLine());
        for (int i = 0; i < N; i++) {
            key = br.readLine();
            alumni.put(key,Integer.parseInt(key.substring(6,14)));
        }
    
        int M=Integer.parseInt(br.readLine());
        for (int i = 0; i < M; i++) {
            key = br.readLine();
            guest.put(key,Integer.parseInt(key.substring(6,14)));
            if(alumni.containsKey(key)){
                alumniGuest.put(key,Integer.parseInt(key.substring(6,14)));
            }
        }
    
        System.out.println(alumniGuest.size());
    
        if(alumniGuest.size()==0){//没有校友来
            System.out.println(getMaxID(guest));
        }else {//有校友来
            System.out.println(getMaxID(alumniGuest));
        }
        inputStreamReader.close();
        br.close();
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月16日
  • 已采纳回答 2月8日
  • 创建了问题 2月5日

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄