c++将成员表按照成绩从高到低排序,即成绩最高的学生为 student[0] (2)查找:输入学生的班级序号,查找对应的成绩 (3)修改操作:输入学生的班级序号,和新成绩,修改成员表对应学生的成绩 (4)输出前10名(rank_10):输出成绩最高的前十名的班级序号和相对应的成绩
时间: 2024-05-08 15:22:24 浏览: 18
以下是一个可能的实现,使用了结构体和快速排序算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 100
typedef struct {
int class_num;
int score;
} Student;
void swap(Student *a, Student *b) {
Student temp = *a;
*a = *b;
*b = temp;
}
int partition(Student arr[], int low, int high) {
int pivot = arr[high].score;
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j].score >= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quicksort(Student arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
int search(Student arr[], int n, int class_num) {
for (int i = 0; i < n; i++) {
if (arr[i].class_num == class_num) {
return arr[i].score;
}
}
return -1;
}
void modify(Student arr[], int n, int class_num, int new_score) {
for (int i = 0; i < n; i++) {
if (arr[i].class_num == class_num) {
arr[i].score = new_score;
break;
}
}
}
void rank_10(Student arr[], int n, int result[][2]) {
quicksort(arr, 0, n - 1);
for (int i = 0; i < 10 && i < n; i++) {
result[i][0] = arr[i].class_num;
result[i][1] = arr[i].score;
}
}
int main() {
Student students[MAX_STUDENTS];
int n;
printf("Enter number of students: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter class number and score for student %d: ", i + 1);
scanf("%d %d", &students[i].class_num, &students[i].score);
}
printf("Student list before sorting:\n");
for (int i = 0; i < n; i++) {
printf("%d %d\n", students[i].class_num, students[i].score);
}
quicksort(students, 0, n - 1);
printf("Student list after sorting:\n");
for (int i = 0; i < n; i++) {
printf("%d %d\n", students[i].class_num, students[i].score);
}
int class_num;
printf("Enter class number to search: ");
scanf("%d", &class_num);
int score = search(students, n, class_num);
if (score == -1) {
printf("Class number not found.\n");
} else {
printf("Score for class %d: %d\n", class_num, score);
}
int new_score;
printf("Enter class number to modify and new score: ");
scanf("%d %d", &class_num, &new_score);
modify(students, n, class_num, new_score);
printf("Student list after modification:\n");
for (int i = 0; i < n; i++) {
printf("%d %d\n", students[i].class_num, students[i].score);
}
int result[10][2];
rank_10(students, n, result);
printf("Top 10 students:\n");
for (int i = 0; i < 10 && result[i][1] > 0; i++) {
printf("%d %d\n", result[i][0], result[i][1]);
}
return 0;
}
```
注意,此实现假定班级序号为正整数且不重复。对于实际应用中可能存在的问题,需要根据具体情况进行修改。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)