任务描述编写C++程序使用C++语言奥运会上有一些综合项目有多个子项目如铁人三项、现代五项、十项全能等。假设这些综合项目的成绩是按照各个子项目的积分之和计算的总积分越高则成绩越好。设每个子项目的积分规则为如下:第1名积10分第2名积7分第3、4、5、6、7名分别5、4、3、2、1分其它名次没有积分。简化起见子项目排名没有并列名次情况。一个综合项目会有多运动员参加。要求:编写一个综合项目计分程序先由
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Athlete {
string name;
vector<double> scores;
int totalScore;
int rank;
};
bool compare(const Athlete& a, const Athlete& b) {
return a.totalScore > b.totalScore;
}
int main() {
int numOfSubProjects, numOfAthletes;
cin >> numOfSubProjects >> numOfAthletes;
vector<Athlete> athletes(numOfAthletes);
for (int i = 0; i < numOfAthletes; i++) {
cin >> athletes[i].name;
athletes[i].scores.resize(numOfSubProjects);
athletes[i].totalScore = 0;
for (int j = 0; j < numOfSubProjects; j++) {
cin >> athletes[i].scores[j];
}
}
for (int i = 0; i < numOfAthletes; i++) {
for (int j = 0; j < numOfSubProjects; 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 - 1);
}
}
}
}
sort(athletes.begin(), athletes.end(), compare);
for (int i = 0; i < numOfAthletes; i++) {
athletes[i].rank = i + 1;
}
for (int i = 0; i < numOfAthletes; i++) {
cout << athletes[i].rank << " " << athletes[i].name << " " << athletes[i].totalScore << endl;
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/hAr1 著作权归作者所有。请勿转载和采集!