c语言学生成绩降序排序
时间: 2023-08-19 08:29:09 浏览: 31
下面是一个简单的 C 代码实现,通过冒泡排序算法将学生成绩按照降序排列:
```c
#include <stdio.h>
int main()
{
int i, j, n, temp;
int arr[100];
printf("请输入学生人数:");
scanf("%d", &n);
printf("请输入学生成绩:\n");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
// 冒泡排序算法
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(arr[j] < arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("按照降序排列后的学生成绩为:\n");
for(i=0; i<n; i++)
printf("%d\n", arr[i]);
return 0;
}
```
该程序首先从用户输入中读取学生人数和每个学生的成绩,然后使用冒泡排序算法将成绩按照降序排列,最后输出结果。
相关问题
根据C语言的成绩降序排列
您好!您可以使用冒泡排序或快速排序来根据C语言成绩降序排列。以下是使用冒泡排序的示例代码:
```c
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
int n;
printf("请输入学生人数:");
scanf("%d", &n);
int scores[n];
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的成绩:", i + 1);
scanf("%d", &scores[i]);
}
bubbleSort(scores, n);
printf("成绩降序排列结果:\n");
for (int i = 0; i < n; i++) {
printf("%d ", scores[i]);
}
printf("\n");
return 0;
}
```
在这个代码中,我们首先读入学生人数和每个学生的成绩,然后使用冒泡排序对成绩进行降序排列,最后输出排列结果。
用结构体存储3个学生成绩降序排序 c语言
好的,以下是实现方式:
```c
#include <stdio.h>
struct student {
char name[20];
float score1;
float score2;
float score3;
float avg_score;
};
void sort(struct student s[], int n);
int main() {
struct student s[3];
int i;
// 输入学生信息
for (i = 0; i < 3; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", s[i].name);
printf("请输入第%d个学生的三门课成绩:", i + 1);
scanf("%f%f%f", &s[i].score1, &s[i].score2, &s[i].score3);
s[i].avg_score = (s[i].score1 + s[i].score2 + s[i].score3) / 3.0; // 计算平均分
}
// 按平均分降序排序
sort(s, 3);
// 输出排序结果
printf("\n按平均分降序排序结果如下:\n");
for (i = 0; i < 3; i++) {
printf("%s的平均分为:%.2f\n", s[i].name, s[i].avg_score);
}
return 0;
}
void sort(struct student s[], int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (s[i].avg_score < s[j].avg_score) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
```
以上代码中,我们首先定义了一个结构体 `student`,包含了学生的姓名和三门课成绩,以及计算得出的平均分。然后在 `main` 函数中,我们输入了三个学生的信息,并且计算出了他们的平均分。接着,我们调用 `sort` 函数对学生信息按照平均分进行降序排序,并输出排序结果。
`sort` 函数采用了简单的冒泡排序算法,对结构体数组 `s` 中的学生信息按照平均分进行降序排序。
相关推荐
![](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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)