L1-005 考试座位号(分数 15)
每个 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
开始我用Java写,思路如下:
用一个字符串数组String stus[index]来保存考生的所有数据:
将考生的试机座位号作为数组的索引index (题干中提到每个学生的试机座位号都不同,所以我就打算用这个来作为每一个学生的标识)
将考生的准考证号和考试座位号存入数组对应index的字符串中
接收要查找的学生的试机座位号index_s,输出对应的stus[index_s]即可。
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test{
public static void main(String[] args) throws IOException {
BufferedReader bRead = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bRead.readLine());
String[] stus = new String[N];
for(int i = 0; i < N; i++) {
String[] data = bRead.readLine().split(" ");
stus[Integer.parseInt(data[1])-1] = data[0] + " " + data[2];
}
int M = Integer.parseInt(bRead.readLine());
String[] index = bRead.readLine().split(" ");
for(int i = 0; i < M; i++) {
System.out.println(stus[Integer.parseInt(index[i])-1]);
}
}
}
但是测试点1和测试点2会因超时无法通过,初步猜测为Scanner导致,但换用BufferedReader后测试点1和2依然超时。在网上找了一圈之后还是解决不了,求问大佬解决办法,求求求求求求🙏🙏🙏🙏
最后直接用C++写了,同样的思路,代码如下:
#include<iostream>
#include<string>
using namespace std;
int main(){
int N,M,index,index_s;
string n;
cin>>N;
string num;
string stus[N];
for(int i = 0; i < N; i++){
cin>>num>>index>>n;
stus[index-1] = num + " " + n;
}
cin>>M;
for(int i = 0; i < M; i++){
cin>>index_s;
cout<<stus[index_s-1];
}
return 0;
}