PAT-B 1041. 考试座位号(15)

来源:互联网 发布:数据库营销 编辑:程序博客网 时间:2024/05/16 11:57

题目链接在此。

这个题目挺直白的,直接看代码好了。

#include<stdio.h>struct info{    char stuId[20];    int tryId;    int seatId;}stu[1001];int main(){    int N;    scanf("%d",&N);    for(int i = 0; i < N; i++){        scanf("%s %d %d",&stu[i].stuId,&stu[i].tryId,&stu[i].seatId);           }    int M;    scanf("%d",&M);    for(int i = 0; i < M; i++){        int seatId;        scanf("%d",&seatId);        for(int j = 0; j < N; j++){            if(stu[j].tryId == seatId){                printf("%s %d\n",stu[j].stuId,stu[j].seatId);                break;            }        }    }    return 0;}

改进方案

这个改进方案是《算法笔记》上的思路,这种改进既节省时间,又节省了空间。
不像上面的代码一样,结构体中有三个变量,而是只有两个(准考证号、座位号),试机座位号作为stu结构体的下标。
这样一来,省去了存储试机座位号的空间,并且不用像上面代码那样遍历stu[]数组去查询,而是直接输出 stu[试机座位号].准考证号、stu[试机座位号].座位号 的对应信息即可。

0 0