PAT:B1041 考试座位号(15 分)
每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。
输入格式:
输入第一行给出一个正整数 N(≤1000),随后 N 行,每行给出一个考生的信息:准考证号 试机座位号 考试座位号
。其中准考证号
由 14 位数字组成,座位从 1 到 N 编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。
考生信息之后,给出一个正整数 M(≤N),随后一行中给出 M 个待查询的试机座位号码,以空格分隔。
输出格式:
对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用 1 个空格分隔。
输入样例:
4
10120150912233 2 4
10120150912119 4 1
10120150912126 1 3
10120150912002 3 2
2
3 4
输出样例:
10120150912002 2
10120150912119 1
思路:
学号 试机作为 考试座位
1.定义一个构造体:分别有 准考证号 试机座位 考试座位
2.定义一个该构造体的数据,去接收所有的数据
3.输入所有学生的数据,每输入一个迟到学生的试机座号,以此输出那个学生的准考证号,考试座位号
代码:
C/C++:
#include<cstdio>
#include<algorithm>
using namespace std;
// 学号, 试机座位号 考试座位号
const int maxn = 1010;
struct Stu{
long long id;
int sj, ks;
}stu[maxn], temp;
int main() {
int n, m, a[maxn];
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%lld %d %d", &temp.id, &temp.sj, &temp.ks);
stu[temp.sj] = temp;
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < m; i++) {
printf("%lld %d\n", stu[a[i]].id, stu[a[i]].ks);
}
return 0;
}
Java:
import java.util.Scanner;
// 一个结果运行超时
class Stu {
String id;
int sj, ks;
public Stu(String id, int sj, int ks) {
this.id = id;
this.sj = sj;
this.ks = ks;
}
public Stu() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getSj() {
return sj;
}
public void setSj(int sj) {
this.sj = sj;
}
public int getKs() {
return ks;
}
public void setKs(int ks) {
this.ks = ks;
}
}
public class Main {
static int maxn = 1010;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N, M, sjNum;
Stu[] all = new Stu[maxn];
Stu temp = new Stu();
N = in.nextInt();
for(int i = 0; i < N; i++) {
temp.setId(in.next());
temp.setSj(in.nextInt());
temp.setKs(in.nextInt());
all[temp.getSj()] = new Stu(temp.getId(), temp.getSj(), temp.getKs());
}
M = in.nextInt();
int[] a = new int[M];
for(int i = 0; i < M; i++) {
sjNum = in.nextInt();
System.out.printf("%s %d\n", all[sjNum].getId(), all[sjNum].getKs());
}
}
}