设计一个算法并用c语言写出参加运动会的n个学校编号为1~n。比赛分成m个男子项目和w个女子项目,项目编号分别为1~m和m+1~m+w。由于各项目参加人数差别较大,有些项目取前五名,得分顺序为7,5,3,2,1;还有些项目只取前三名,得分顺序为5,3,2。哪些项目取前五名或前三名由学生自己设定。写一个统计程序产生各种成绩单和得分报表。 基本要求: (1)各项目结束时,输入前三名或前五名的项目编号、运动员姓名、校名和名次(成绩); (2)产生各学校的成绩单,内容包括每个学校所取得的每项成绩的项目号、名次(成绩)、姓名和得分,并统计各学校总分; (3)可以按学校编号、男女团体总分排序输出; (4)可以按学校编号查询学校某个项目的情况; (5)可以按项目编号查询取得前三或前五名的学校。
时间: 2024-03-15 11:46:21 浏览: 67
以下是一个简单的实现,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SCHOOLS 100
#define MAX_EVENTS 20
#define MAX_NAME_LEN 20
typedef struct {
int school_id;
char name[MAX_NAME_LEN];
} Athlete;
typedef struct {
int event_id;
int max_rank;
int scores[5];
} Event;
typedef struct {
int school_id;
char name[MAX_NAME_LEN];
int scores[MAX_EVENTS];
int total_score;
} School;
Athlete athletes[MAX_SCHOOLS * 10];
int num_athletes = 0;
Event events[MAX_EVENTS];
int num_events = 0;
School schools[MAX_SCHOOLS];
int num_schools = 0;
void add_athlete(int school_id, char *name) {
athletes[num_athletes].school_id = school_id;
strcpy(athletes[num_athletes].name, name);
num_athletes++;
}
void add_event(int event_id, int max_rank, int *scores) {
events[num_events].event_id = event_id;
events[num_events].max_rank = max_rank;
memcpy(events[num_events].scores, scores, max_rank * sizeof(int));
num_events++;
}
void add_school(int school_id, char *name) {
schools[num_schools].school_id = school_id;
strcpy(schools[num_schools].name, name);
memset(schools[num_schools].scores, 0, num_events * sizeof(int));
schools[num_schools].total_score = 0;
num_schools++;
}
int sort_schools_by_id(const void *a, const void *b) {
return ((School *)a)->school_id - ((School *)b)->school_id;
}
int sort_schools_by_score(const void *a, const void *b) {
return ((School *)b)->total_score - ((School *)a)->total_score;
}
void print_school(int school_id) {
int i, j;
for (i = 0; i < num_schools; i++) {
if (schools[i].school_id == school_id) {
printf("%s:\n", schools[i].name);
for (j = 0; j < num_events; j++) {
printf("Event %d: %d\n", events[j].event_id, schools[i].scores[j]);
}
printf("Total score: %d\n", schools[i].total_score);
return;
}
}
printf("School not found.\n");
}
void print_event(int event_id, int max_rank) {
int i, j;
printf("Event %d:\n", event_id);
for (i = 0; i < num_schools; i++) {
for (j = 0; j < num_athletes; j++) {
if (athletes[j].school_id == schools[i].school_id) {
if (events[event_id].max_rank == max_rank) {
if (schools[i].scores[event_id] == events[event_id].scores[0] ||
schools[i].scores[event_id] == events[event_id].scores[1] ||
schools[i].scores[event_id] == events[event_id].scores[2]) {
printf("%s %d %d\n", schools[i].name, j + 1, schools[i].scores[event_id]);
break;
}
} else {
if (schools[i].scores[event_id] == events[event_id].scores[0] ||
schools[i].scores[event_id] == events[event_id].scores[1] ||
schools[i].scores[event_id] == events[event_id].scores[2] ||
schools[i].scores[event_id] == events[event_id].scores[3] ||
schools[i].scores[event_id] == events[event_id].scores[4]) {
printf("%s %d %d\n", schools[i].name, j + 1, schools[i].scores[event_id]);
break;
}
}
}
}
}
}
int main() {
int n, m, w, i, j, k, school_id, event_id, rank, score;
char name[MAX_NAME_LEN];
scanf("%d %d %d", &n, &m, &w);
for (i = 1; i <= n; i++) {
scanf("%d %s", &school_id, name);
add_school(school_id, name);
}
for (i = 1; i <= m + w; i++) {
scanf("%d %d", &event_id, &j);
int scores[5] = {0};
for (k = 0; k < j; k++) {
scanf("%d", &scores[k]);
}
add_event(event_id, j, scores);
}
while (scanf("%d %s %d %d", &event_id, name, &school_id, &rank) == 4) {
for (i = 0; i < num_athletes; i++) {
if (strcmp(athletes[i].name, name) == 0 && athletes[i].school_id == school_id) {
int event_index = -1;
for (j = 0; j < num_events; j++) {
if (events[j].event_id == event_id) {
event_index = j;
break;
}
}
if (event_index != -1) {
schools[school_id - 1].scores[event_index] = events[event_index].scores[rank - 1];
break;
}
}
}
}
for (i = 0; i < num_athletes; i++) {
int school_index = athletes[i].school_id - 1;
for (j = 0; j < num_events; j++) {
schools[school_index].total_score += schools[school_index].scores[j];
}
}
qsort(schools, num_schools, sizeof(School), sort_schools_by_id);
for (i = 0; i < num_schools; i++) {
printf("School %d: %s\n", schools[i].school_id, schools[i].name);
for (j = 0; j < num_events; j++) {
printf("Event %d: %d\n", events[j].event_id, schools[i].scores[j]);
}
printf("Total score: %d\n", schools[i].total_score);
}
qsort(schools, num_schools, sizeof(School), sort_schools_by_score);
for (i = 0; i < num_schools; i++) {
printf("School %d: %s, total score: %d\n", schools[i].school_id, schools[i].name, schools[i].total_score);
}
scanf("%d", &school_id);
print_school(school_id);
scanf("%d", &event_id);
print_event(event_id, events[event_id - 1].max_rank);
return 0;
}
```
该程序使用了三个结构体:`Athlete` 表示运动员,`Event` 表示比赛项目,`School` 表示学校。在程序开始时,先读入学校和比赛项目的信息,并存储在数组中。然后,每当有运动员获得名次时,就更新该运动员所属学校在该项目上的得分。最后,对每个学校的得分进行统计,并按照学校编号或总分进行排序,输出各种成绩单和得分报表。其中,查询学校某个项目的情况和查询取得前三或前五名的学校的功能都是通过遍历运动员、比赛项目和学校来实现的。
阅读全文