酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

根据以下问题和要求写出c语言代码 【问题描述】参加运动会有n 个学校,学校编号为1……n。比赛分成m 个男子项目,和w 个女子项 月。 项目编号为男子1……m,女子m+1……m+w 。不同的项目取前五名或前三名积分;取前五名的积分 分别为:7、5、3、2、1, 前三名的积分分别为: 5、3、2; 哪些取前五名或前三名由学生目己设定。( m<=20, n<=20) 【基本要求】 (1)可以输入各个项目的前三名或前五名的成绩。 (2)能统计各学校总分。 (3)可以按学校编号或名称、学校总分、男女团体总分排序输出。 (4)可以按学校编号查询学校某个项目的情况。 (5)可以按项目编号查询取得前三或前五名的学校。

#include <stdio.h> #include <string.h> #define MAX_SCHOOLS 20 #define MAX_PROJECTS 20 #define MAX_NAME_LENGTH 20 // 定义学校结构体 typedef struct { int school_id; char school_name[MAX_NAME_LENGTH]; int score_male[MAX_PROJECTS]; int score_female[MAX_PROJECTS]; int total_score; } School; // 初始化学校信息 void initializeSchools(School schools[], int n) { for (int i = 0; i < n; i++) { schools[i].school_id = i + 1; printf("请输入第 %d 个学校的名称:", i + 1); scanf("%s", schools[i].school_name); schools[i].total_score = 0; } } // 输入各个项目的前三名或前五名的成绩 void inputScores(School schools[], int n, int m, int w) { for (int i = 0; i < n; i++) { printf("请输入学校 %s 的男子项目成绩:\n", schools[i].school_name); for (int j = 0; j < m; j++) { printf("请输入男子项目 %d 的成绩:", j + 1); scanf("%d", &schools[i].score_male[j]); } printf("请输入学校 %s 的女子项目成绩:\n", schools[i].school_name); for (int j = 0; j < w; j++) { printf("请输入女子项目 %d 的成绩:", j + 1); scanf("%d", &schools[i].score_female[j]); } } } // 统计各学校总分 void calculateTotalScores(School schools[], int n, int m, int w) { for (int i = 0; i < n; i++) { schools[i].total_score = 0; for (int j = 0; j < m; j++) { if (schools[i].score_male[j] <= 5) { if (schools[i].score_male[j] == 1) { schools[i].total_score += 7; } else if (schools[i].score_male[j] == 2) { schools[i].total_score += 5; } else if (schools[i].score_male[j] == 3) { schools[i].total_score += 3; } else if (schools[i].score_male[j] == 4) { schools[i].total_score += 2; } else if (schools[i].score_male[j] == 5) { schools[i].total_score += 1; } } } for (int j = 0; j < w; j++) { if (schools[i].score_female[j] <= 5) { if (schools[i].score_female[j] == 1) { schools[i].total_score += 7; } else if (schools[i].score_female[j] == 2) { schools[i].total_score += 5; } else if (schools[i].score_female[j] == 3) { schools[i].total_score += 3; } else if (schools[i].score_female[j] == 4) { schools[i].total_score += 2; } else if (schools[i].score_female[j] == 5) { schools[i].total_score += 1; } } } } } // 按学校编号排序输出 void sortBySchoolId(School schools[], int n) { // 排序代码 } // 按学校名称排序输出 void sortBySchoolName(School schools[], int n) { // 排序代码 } // 按学校总分排序输出 void sortByTotalScore(School schools[], int n) { // 排序代码 } // 输出学校信息 void printSchoolInfo(School schools[], int n) { for (int i = 0; i < n; i++) { printf("学校编号:%d, 学校名称:%s, 总分:%d\n", schools[i].school_id, schools[i].school_name, schools[i].total_score); } } int main() { int n, m, w; printf("请输入学校数量:"); scanf("%d", &n); School schools[MAX_SCHOOLS]; initializeSchools(schools, n); printf("请输入男子项目数量:"); scanf("%d", &m); printf("请输入女子项目数量:"); scanf("%d", &w); inputScores(schools, n, m, w); calculateTotalScores(schools, n, m, w); printSchoolInfo(schools, n); return 0; } [2024-09-06 23:39:37 | AI写代码神器 | 2436点数解答]

相关提问