1041. 考试座位号
每个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
方法一:
用结构体存每个学生的数据,再创一个数组s,用来存从试机号反应他是第几个学生,s[试机号]=i,在查询时,输入试机号m,s[m]就代表他是第几个(i),再stu[i]
#include<bits/stdc++.h>
using namespace std;
struct student{
string id;
int a,b;
}stu[1005];
int s[1005];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>stu[i].id>>stu[i].a>>stu[i].b;
s[stu[i].a]=i;
}
int k;
cin>>k;
for(int i=0;i<k;i++){
int m;cin>>m;
cout<<stu[s[m]].id<<" "<<stu[s[m]].b<<endl;
}
return 0;
}
方法二:
建⽴string类型的⼆维数组stu[1005][2]
,假设试机座位号为t,将输⼊得到的学⽣准考证号s1保stu[t][0]
存在中,考试座位号s2保存在stu[t][2]
中,对于查询的m个学⽣,已知试机座位号t,则直接输出stu[t][0]
和stu[t][1]
#include <iostream>
using namespace std;
int main() {
string stu[1005][2], s1, s2;;
int n, m, t;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> s1 >> t >> s2;
stu[t][0] = s1;
stu[t][1] = s2;
}
cin >> m;
for(int i = 0; i < m; i++) {
cin >> t;
cout << stu[t][0] << " " << stu[t][1] << endl;
}
return 0;
}