有十个评委打分,评委输入分数,比平均分高的就输出. c++
时间: 2024-10-14 21:16:20 浏览: 27
在 C++ 中,你可以通过以下几个步骤创建一个简单的程序来实现这个功能:
1. 首先,定义一个结构体或类来表示评委和他们的分数,例如 `Judge` 结构体:
```cpp
struct Judge {
int id; // 评委编号
int score; // 打分
};
```
2. 定义一个函数计算所有评委的平均分,并存储最高分的选手信息:
```cpp
double calculateAverage(const std::vector<Judge>& judges) {
double sum = 0;
for (const auto& judge : judges) {
sum += judge.score;
}
double average = sum / judges.size();
return average;
}
Judge highestScorer(const std::vector<Judge>& judges) {
Judge maxScoreJudge;
maxScoreJudge.score = judges[0].score;
for (const auto& judge : judges) {
if (judge.score > maxScoreJudge.score) {
maxScoreJudge = judge;
}
}
return maxScoreJudge;
}
```
3. 主函数中获取评委分数并进行比较:
```cpp
int main() {
std::vector<Judge> judges;
// 接收评委分数
for (size_t i = 0; i < 10; ++i) {
int score;
std::cin >> score;
judges.push_back({i + 1, score}); // 从 1 开始编号
}
double average = calculateAverage(judges);
Judge winner = highestScorer(judges);
if (winner.score > average) {
std::cout << "评委 " << winner.id << " 的分数高于平均分:" << winner.score << std::endl;
} else {
std::cout << "没有任何评委的分数高于平均分" << std::endl;
}
return 0;
}
```
在这个程序中,首先读取10位评委的分数,然后计算平均分,最后找出得分最高的评委(如果有的话)并判断其是否高于平均分。
阅读全文