L1-005 考试座位号(java)

1. 题目描述:

每个 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

2. 思路讲解

看到这道题我就想到了线性表,要把数据放在一个线性表中,定义一个学生类,将学生的准考证号 试机座位号 考试座位号放入,然后根据试机座位号排序,最后取出数据只需要根据相应的序号取出数据即可


3. 代码示例:

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

/**
 * 考生类
 */
class Student{
    //准考证号
    String id;
    //试机座位号
    String computerId;
    //考试座位号
    String testId;

    public Student(String id, String computerId, String testId) {
        this.id = id;
        this.computerId = computerId;
        this.testId = testId;
    }

    @Override
    public String toString() {
        return id + " " + testId;
    }
}
class stuCmp implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        //按照学生的试机座位号排序
        return o1.computerId.compareTo(o2.computerId);
    }
}
public class 考试座位号 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        //输入n
        //read()
        //Reads a single character.
        int n = Integer.parseInt(bf.readLine());
        //定义一个存放考生信息的链表
        List<Student> students = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            String[] line = bf.readLine().split(" ");
            students.add(new Student(line[0], line[1], line[2]));
        }
        //给学生进行排序
        students.sort(new stuCmp());
        //输入待查询的试机座位号码
        int m = Integer.parseInt(bf.readLine());
        String[] comIds = bf.readLine().split(" ");
        for (int i = 0; i < m; i++) {
            int comId = Integer.parseInt(comIds[i]);
            System.out.println(students.get(comId - 1).toString());
        }
    }
}

但是这个代码只通过了两个测试点,有11分,还有两个测试点运行超时

那么可不可以再省略一些步骤呢,可不可以把排序省略掉,直接按照考生的试机座位号将考生的准考证号和考试座位号放入线性表中,因为最后输出只需要输出这两个信息,即将考生的试机座位号作为线性表的序号依次放入数据


4. 代码改进:

这时便不可以再使用线性表存放数据了,因为线性表存放的数据是按照顺序存储数据,所以这里我用到了HashMap存放数据,将考生试机座位号作为键,考生的准考证号和考试座位号作为值,发现还是超时, 到最后我改成了只用数组存放数据,结果还是超时, 那就有可能是Java语言本身就是存在着运行时间过长的

import java.io.*;

public class 考试座位号 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        //输入n
        int n = Integer.parseInt(bf.readLine());
        String[] students = new String[n];
        for (int i = 0; i < n; i++) {
            String[] line = bf.readLine().split(" ");
            //插入数据
            students[Integer.parseInt(line[1]) - 1] = line[0] + " " + line[2];
        }
        //输入待查询的试机座位号码
        int m = Integer.parseInt(bf.readLine());
        String[] comIds = bf.readLine().split(" ");
        for (int i = 0; i < m; i++) {
            int comId = Integer.parseInt(comIds[i]);
            //根据考生的试机座位号码直接获取考生信息
            System.out.println(students[comId - 1]);
        }
    }
}


更新

我在后面的练习中发现字符串的连接方式不同,代码运行的速度也不同,我们知道,String字符串虽然是不可变字符串,但可以进行拼接产生新的对象。String字符串拼接可以使用“+”运算符,也可以使用concat()方法,不同的是"+"可以连接任何类型的数据形成字符串, concat()只能拼接String类型的字符串

只需要将上面插入数据部分的代码改成这样, 就可以通过了:

        for (int i = 0; i < n; i++) {
            String[] line = bf.readLine().split(" ");
            //插入数据
            String s = line[0].concat(" ").concat(line[2]);
            students[Integer.parseInt(line[1]) - 1] = s;
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lilianac

你的鼓励是我的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值