每个 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
知识点:
动态链表
程序代码:
#include <stdio.h>
#include <malloc.h>
struct Student *creat(int n);
void Search(int x[],int n,int m,struct Student *head);
struct Student{
long long id;
int seat;
int textseat;
struct Student *next;
};
int main()
{
int N,M;
struct Student *head;
scanf("%d",&N);
head=creat(N);
//查找数量
scanf("%d",&M);
//开始查找
int x[M],i;
for (i=0;i<M;i++)
{
scanf("%d",&x[i]);
}
Search(x,N,M,head);
return 0;
}
struct Student *creat(int n)
{
struct Student *head,*p1,*p2;
int t=0;
p1=(struct Student*)malloc(sizeof(struct Student));
scanf("%lld%d%d",&p1->id,&p1->seat,&p1->textseat);
while(n)
{
t++;
if (t==1)
{
head=p1;
p2=p1;
p1->next=NULL;
}
else
{
p1=(struct Student*)malloc(sizeof(struct Student));
scanf("%lld%d%d",&p1->id,&p1->seat,&p1->textseat);
p2->next=p1;
p2=p1;
p1->next=NULL;
}
n--;
}
return head;
}
void Search(int x[],int n,int m,struct Student *head)
{
struct Student *p;
int i,j;
p=head;
for(i=0;i<m;i++)
{
while(p!=NULL)
{
if (x[i]==p->seat)
{
printf("%lld %d",p->id,p->textseat);
putchar('\n');
}
p=p->next;
}
p=head;
}
}