PAT1041 考试座位号(C语言实现,利用hash表实现快速查找)

题目链接:1041 考试座位号
思路:
创建结构体,存放考生号与座位号。建立数组,在输入信息时,把机试号作为数组的下标index值。在打印时直接可打印,不用进行for循环查找。

#include 
#include 
struct Student {
	char num[20];//考生号
	int sit;//座位号
};
struct Student stu[1010];
int main() {
	char temp[20];
	int n, i, k;
	int m, t;
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		//利用机试号作为hash表的key值
		scanf("%s %d", temp, &k);
		strcpy(stu[k].num,temp);
		scanf("%d", &stu[k].sit);
	}
	scanf("%d", &m);
	for (i = 0; i < m; i++) {
		scanf("%d", &t);
		printf("%s %d\n", stu[t].num, stu[t].sit);
	}
	return 0;
}

你可能感兴趣的:(C语言)