Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2302580024 颜云珍 #48

Open
yanyz0 opened this issue Mar 11, 2024 · 0 comments
Open

2302580024 颜云珍 #48

yanyz0 opened this issue Mar 11, 2024 · 0 comments

Comments

@yanyz0
Copy link

yanyz0 commented Mar 11, 2024

#include <stdio.h>

// 定义结构体存储学生信息
typedef struct student {
int pl; // 学号
float score; // 成绩
} Student;

// 排序函数,按成绩由高到低排序
void sort_students(Student arr[], int n) {
int i, j;
float temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i].score < arr[j].score) {
temp = arr[i].score;
arr[i].score = arr[j].score;
arr[j].score = temp;
}
}
}
}

// 计算平均成绩和不及格人数
void calculate(Student arr[], int n) {
int i, count = 0;
float sum = 0.0;
for (i = 0; i < n; i++) {
sum += arr[i].score;
if (arr[i].score < 60) { // 不及格分数为60分以下
count++;
}
}
float average = sum / n;
printf("平均成绩为:%.2f\n", average);
printf("不及格学生人数为:%d\n", count);
}

int main() {
int n, i;
Student arr[100]; // 假设最多有100个学生
printf("请输入班级学生人数:");
scanf("%d", &n);
printf("请输入学生的学号和成绩(空格分隔):\n");
for (i = 0; i < n; i++) {
scanf("%d %f", &arr[i].pl, &arr[i].score);
}
sort_students(arr, n); // 排序
calculate(arr, n); // 计算平均成绩和不及格人数
printf("按成绩由高到低排序的学生信息:\n");
printf("学号 成绩\n");
for (i = 0; i < n; i++) {
printf("%d %.2f\n", arr[i].pl, arr[i].score);
}
return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant