int SearchByroom(struct student stu[],int n,int value) { int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; if (stu[mid].room == value) { return mid; } else if (stu[mid].room > value) { right = mid-1; } else { left = mid+1; } } return -1; } int SearchByroom(struct student stu[],int n,int value) { int left = 0, right = n - 1; while (left <= right) { int mid = (left + right) / 2; if (stu[mid].room == value) { return mid; } else if (stu[mid].room > value) { right = mid-1; } else { left = mid+1; } } return -1; } printf("要查找的房间\n"); int room; scanf("%d",&room); getchar(); int indexb; indexb=SearchByroom(stu, n, room); if(indexb !=-1) { printf("查询结果:"); for(int i=indexb; i<n && stu[i].room==room; i++) { printf("%s %s %d\n", stu[i].name, stu[i].id, stu[i].room); } } else { printf("没有找到该学生\n"); }break; 没有实现要查询到同一房间的所有人
时间: 2024-03-21 20:41:08 浏览: 55
int-to-bits.rar_Int.intToIntBits_int-bits_int赋值给位域_整数存入位域
抱歉,我之前的回答有误。要实现查询同一房间的所有人,可以在查找到房间号对应的第一个学生后,使用一个循环语句输出同一房间的所有学生信息,直到房间号不再匹配为止。代码示例如下:
```
int SearchByroom(struct student stu[], int n, int value) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (stu[mid].room == value) {
return mid;
} else if (stu[mid].room > value) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
printf("要查找的房间\n");
int room;
scanf("%d", &room);
getchar();
int indexb;
indexb = SearchByroom(stu, n, room);
if (indexb != -1) {
printf("查询结果:\n");
// 输出同一房间的所有学生信息
for (int i = indexb; i < n && stu[i].room == room; i++) {
printf("%s %s %d\n", stu[i].name, stu[i].id, stu[i].room);
}
} else {
printf("没有找到该房间的学生\n");
}
```
这样就可以实现查询同一房间的所有人了。
阅读全文