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

2302580027,游志新 #46

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

2302580027,游志新 #46

Youzhix opened this issue Mar 11, 2024 · 0 comments

Comments

@Youzhix
Copy link

Youzhix commented Mar 11, 2024

#include <stdio.h>

typedef struct {
    int studentID;
    float score;
} Student;

int compare(const void *a, const void *b) {
    Student *studentA = (Student *)a;
    Student *studentB = (Student *)b;
    if (studentA->score < studentB->score) {
        return 1;
    } else if (studentA->score > studentB->score) {
        return -1;
    } else {
        return 0;
    }
}

int main() {
    int n; // 学生人数
    printf("请输入学生人数:");
    scanf("%d", &n);

    Student students[n]; // 学生数组

    for (int i = 0; i < n; i++) {
        printf("请输入第%d个学生的学号和成绩:", i + 1);
        scanf("%d%f", &students[i].studentID, &students[i].score);
    }

    qsort(students, n, sizeof(Student), compare);

    printf("按成绩由高到低的顺序输出学生的学号和成绩:\n");
    for (int i = 0; i < n; i++) {
        printf("学号:%d,成绩:%.2f\n", students[i].studentID, students[i].score);
    }

    float sum = 0;
    int count = 0;
    for (int i = 0; i < n; i++) {
        sum += students[i].score;
        if (students[i].score < 60) {
            count++;
        }
    }
    float average = sum / n;

    printf("平均成绩:%.2f\n", average);
    printf("不及格学生人数:%d\n", count);

    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