L1-005 考试座位号

每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。

输入格式:

输入第一行给出一个正整数 N(≤1000),随后 N 行,每行给出一个考生的信息:准考证号 试机座位号 考试座位号。其中准考证号由 16 位数字组成,座位从 1 到 N 编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。

考生信息之后,给出一个正整数 M(≤N),随后一行中给出 M 个待查询的试机座位号码,以空格分隔。

输出格式:

对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用 1 个空格分隔。

输入样例:

4
3310120150912233 2 4
3310120150912119 4 1
3310120150912126 1 3
3310120150912002 3 2
2
3 4

输出样例:

3310120150912002 2
3310120150912119 1

代码长度限制

16 KB

时间限制

200 ms

内存限制

64 MB

上解答:

第一种:

import java.util.Scanner;
public class t5 {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        long[] l = new long[num];
        int[] n = new int[num];
        int[] m = new int[num];
        for (int i=0;i<num;i++){
            l[i] = sc.nextLong();
            n[i] = sc.nextInt();
            m[i] = sc.nextInt();
        }
        int number = sc.nextInt();
        for (int i=0;i<number;i++){
            int a = sc.nextInt();
            for (int j=0;j<num;j++){
                if (a==n[j]){
                    System.out.println(l[j]+" "+m[j]);
                }
            }
        }
    }
}

注意:使用这种方式会超时,原因是scanner()读入数据比较慢;

第二种:

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


public class t5 {
    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int snum = Integer.parseInt(in.readLine());
        HashMap<Integer, String> student = new HashMap<>();
        for (int i = 0; i < snum; i++) {
            String[] str = in.readLine().split(" ");
            int key = Integer.parseInt(str[1]);
            String s = str[0]+" "+str[2];
            student.put(key, s);
        }
        int sm = Integer.parseInt(in.readLine());
        String[] str = in.readLine().split(" ");
        for (int i = 0; i < sm; i++) {
            int m_key = Integer.parseInt(str[i]);
            System.out.println(student.get(m_key));
        }
    }
}

 注意:这种方式解决了读入数据使用scanner()读入读入慢的问题,但是并不是最优,还会有两个测试点不通过,原因是在字符串的连接上:String s = str[0]+" "+str[2];字符串在连接时使用"+"号连接,不会区分要连接的数据类型,速度较慢;

第三种:

public class t5 {
    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int snum = Integer.parseInt(in.readLine());
        HashMap<Integer, String> student = new HashMap<>();
        for (int i = 0; i < snum; i++) {
            String[] str = in.readLine().split(" ");
            int key = Integer.parseInt(str[1]);
            String s = str[0].concat(" ").concat(str[2]);
            student.put(key, s);
        }
        int sm = Integer.parseInt(in.readLine());
        String[] str = in.readLine().split(" ");
        for (int i = 0; i < sm; i++) {
            int m_key = Integer.parseInt(str[i]);
            System.out.println(student.get(m_key));
        }
    }
}

 注意:这里将:String s = str[0]+" "+str[2];  换为:String s = str[0].concat(" ").concat(str[2]);这是因为concat()在连接字符串时只能是String类型的,相比于使用”+”节省时间;与最初的相比,将scanner()换为BufferedReader(),"+"换为concat(),这两者都有优化,节约时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康康好好学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值