C++ 综合项目计分程序 - 奥运会铁人三项、现代五项等
以下是用 C++ 编写的综合项目计分程序,用于计算奥运会综合项目(如铁人三项、现代五项)的运动员得分和排名:
#include <iostream>
#include <vector>
#include <algorithm>
struct Athlete {
std::string name;
std::vector<float> scores;
int totalScore;
int rank;
};
bool compareAthlete(const Athlete& a, const Athlete& b) {
return a.totalScore > b.totalScore;
}
int main() {
int numSubProjects, numAthletes;
std::cout << '请输入综合项目的子项目个数:';
std::cin >> numSubProjects;
std::cout << '请输入参赛运动员数目:';
std::cin >> numAthletes;
std::vector<Athlete> athletes(numAthletes);
for (int i = 0; i < numAthletes; i++) {
std::cout << '请输入第' << i + 1 << '个运动员的姓名:';
std::cin >> athletes[i].name;
athletes[i].scores.resize(numSubProjects);
athletes[i].totalScore = 0;
for (int j = 0; j < numSubProjects; j++) {
std::cout << '请输入第' << j + 1 << '个子项目的成绩:';
std::cin >> athletes[i].scores[j];
}
}
for (int i = 0; i < numAthletes; i++) {
for (int j = 0; j < numSubProjects; j++) {
if (j < 7) {
if (j == 0) {
athletes[i].totalScore += 10;
} else if (j == 1) {
athletes[i].totalScore += 7;
} else {
athletes[i].totalScore += 5 - (j - 2);
}
}
athletes[i].totalScore += athletes[i].scores[j];
}
}
std::sort(athletes.begin(), athletes.end(), compareAthlete);
for (int i = 0; i < numAthletes; i++) {
athletes[i].rank = i + 1;
}
std::cout << '参赛运动员信息(排名 姓名 总积分):' << std::endl;
for (const auto& athlete : athletes) {
std::cout << athlete.rank << ' ' << athlete.name << ' ' << athlete.totalScore << std::endl;
}
return 0;
}
该程序首先要求用户输入综合项目的子项目个数和参赛运动员数目。然后,依次输入每个运动员的姓名和各个子项目的成绩。接下来,根据成绩和积分规则计算每个运动员的总积分,然后按照总积分排序并计算每个运动员的排名。最后,按照排名高低输出参赛运动员的信息。
注意,这个程序中的积分规则是根据题目要求设定的,如果有其他要求,可以根据实际情况进行修改。
原文地址: https://www.cveoy.top/t/topic/o4lD 著作权归作者所有。请勿转载和采集!