使用c语言编程 参加运动会有 n 个学校,学校编号为....比赛分成 m 个男子 项目,和 w 个女子项目。项目编号为男子 1....m.女子 m....mn+w.不同的项目取前五名或前三名积分:取前五名的 积分分别为: 7. 5、3、2、1,前三名的积分分别为: 5、3、2; 哪些取前五名或前三名由学生自己设定。(m<=20, n<=20) [任务要求] 功能要求: 1). 可以输入各个项目的前三名或前五名的成绩; 2).能统计各学校总分; 3).可以按学校编号、学校总分、男女团体总分排序输出; 4).可以按学校编号查询学校某个项目的情况:可以按项目编 号查询取得前三或前五名的学校 规定:输入数据形式和范围: 20 以内的整数(如果做得更好可 以输入学校的名称,运动项目的名称) 输出形式:有中文提示,各学校分数为整型。
时间: 2023-06-16 17:07:24 浏览: 142
C语言编程-编写函数fun求1!+2!+3!+ …… +n!的和,在main函数中由键盘输入n值,并输出运算结果;
5星 · 资源好评率100%
这个题目比较复杂,需要使用结构体和排序算法来实现。以下是一个简单的实现,仅供参考:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SCHOOLS 20
#define MAX_PROJECTS 40
#define MAX_NAME_LENGTH 20
typedef struct {
int id;
char name[MAX_NAME_LENGTH];
int scores[MAX_PROJECTS][5]; // 前五名成绩
int total_score;
int male_score;
int female_score;
} School;
School schools[MAX_SCHOOLS];
int num_schools, num_male_projects, num_female_projects;
// 比较函数,用于排序
int cmp(const void* a, const void* b) {
School* s1 = (School*)a;
School* s2 = (School*)b;
if (s1->total_score != s2->total_score) {
return s2->total_score - s1->total_score;
} else if (s1->male_score != s2->male_score) {
return s2->male_score - s1->male_score;
} else if (s1->female_score != s2->female_score) {
return s2->female_score - s1->female_score;
} else {
return s1->id - s2->id;
}
}
// 查询学校某个项目的前三或前五名
void query_project(int project_id, int top_num) {
printf("请输入项目 %d 的前 %d 名成绩:\n", project_id, top_num);
for (int i = 0; i < num_schools; i++) {
printf("学校 %d (%s):\n", schools[i].id, schools[i].name);
for (int j = 0; j < top_num; j++) {
scanf("%d", &schools[i].scores[project_id][j]);
}
}
}
// 统计各学校总分、男女团体总分
void calculate_scores() {
for (int i = 0; i < num_schools; i++) {
schools[i].total_score = 0;
schools[i].male_score = 0;
schools[i].female_score = 0;
for (int j = 1; j <= num_male_projects; j++) {
for (int k = 0; k < 5; k++) {
if (schools[i].scores[j][k] > 0) {
if (k == 0) {
schools[i].total_score += 7;
schools[i].male_score += 7;
} else if (k == 1) {
schools[i].total_score += 5;
schools[i].male_score += 5;
} else if (k == 2) {
schools[i].total_score += 3;
schools[i].male_score += 3;
} else if (k == 3) {
schools[i].total_score += 2;
schools[i].male_score += 2;
} else if (k == 4) {
schools[i].total_score += 1;
schools[i].male_score += 1;
}
}
}
}
for (int j = num_male_projects + 1; j <= num_male_projects + num_female_projects; j++) {
for (int k = 0; k < 5; k++) {
if (schools[i].scores[j][k] > 0) {
if (k == 0) {
schools[i].total_score += 7;
schools[i].female_score += 7;
} else if (k == 1) {
schools[i].total_score += 5;
schools[i].female_score += 5;
} else if (k == 2) {
schools[i].total_score += 3;
schools[i].female_score += 3;
} else if (k == 3) {
schools[i].total_score += 2;
schools[i].female_score += 2;
} else if (k == 4) {
schools[i].total_score += 1;
schools[i].female_score += 1;
}
}
}
}
}
}
int main() {
printf("请输入学校数量、男子项目数量、女子项目数量:\n");
scanf("%d%d%d", &num_schools, &num_male_projects, &num_female_projects);
for (int i = 0; i < num_schools; i++) {
printf("请输入学校 %d 的名称:\n", i + 1);
scanf("%s", schools[i].name);
schools[i].id = i + 1;
}
for (int i = 1; i <= num_male_projects + num_female_projects; i++) {
int top_num;
printf("请输入项目 %d 的前几名积分:\n", i);
scanf("%d", &top_num);
query_project(i, top_num);
}
calculate_scores();
printf("按什么方式排序输出?(1: 学校编号, 2: 学校总分, 3: 男女团体总分):\n");
int sort_type;
scanf("%d", &sort_type);
if (sort_type == 1) {
qsort(schools, num_schools, sizeof(School), cmp);
printf("按学校编号排序输出:\n");
} else if (sort_type == 2) {
qsort(schools, num_schools, sizeof(School), cmp);
printf("按学校总分排序输出:\n");
} else if (sort_type == 3) {
qsort(schools, num_schools, sizeof(School), cmp);
printf("按男女团体总分排序输出:\n");
}
for (int i = 0; i < num_schools; i++) {
printf("学校 %d (%s):\n", schools[i].id, schools[i].name);
printf(" 总分:%d\n", schools[i].total_score);
printf(" 男团总分:%d\n", schools[i].male_score);
printf(" 女团总分:%d\n", schools[i].female_score);
}
printf("请输入学校编号和项目编号查询某个项目的情况(输入 0 0 结束):\n");
int school_id, project_id;
while (1) {
scanf("%d%d", &school_id, &project_id);
if (school_id == 0 && project_id == 0) {
break;
}
printf("学校 %d (%s) 在项目 %d 中的成绩为:\n", school_id, schools[school_id - 1].name, project_id);
for (int i = 0; i < 5; i++) {
printf(" 第 %d 名:%d\n", i + 1, schools[school_id - 1].scores[project_id][i]);
}
}
return 0;
}
```
阅读全文