定义一个学生结构体,含学号(一维字符数组)、姓名、成绩(整型)。从键盘输入n(n<20),再输入n个学生的信息,按学生成绩从小到大顺序输出学生信息。
时间: 2023-03-26 14:05:00 浏览: 1421
好的,以下是用中文回复:
首先,我们可以定义一个学生结构体,包含学号、姓名和成绩三个属性:
```
struct Student {
char id[20];
char name[20];
int score;
};
```
然后,从键盘输入一个整数n,表示接下来要输入n个学生的信息:
```
int n;
scanf("%d", &n);
```
接着,我们可以定义一个长度为n的学生数组,用来存储这n个学生的信息:
```
Student students[20];
```
然后,使用循环从键盘输入每个学生的信息,并将其存储到数组中:
```
for (int i = ; i < n; i++) {
scanf("%s %s %d", students[i].id, students[i].name, &students[i].score);
}
```
最后,我们可以使用快速排序算法对学生数组按照成绩从小到大排序,并输出每个学生的信息:
```
void quick_sort(Student arr[], int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
int pivot = arr[left + (right - left) / 2].score;
while (i <= j) {
while (arr[i].score < pivot) {
i++;
}
while (arr[j].score > pivot) {
j--;
}
if (i <= j) {
Student temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
quick_sort(arr, left, j);
quick_sort(arr, i, right);
}
quick_sort(students, , n - 1);
for (int i = ; i < n; i++) {
printf("%s %s %d\n", students[i].id, students[i].name, students[i].score);
}
```
这样,我们就完成了从键盘输入n个学生信息,并按照成绩从小到大输出学生信息的任务。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)