> 知识 >

用二维数组编程序:歌曲大赛,有M个评委给N个选手打分,求每个选手的平均得分(去掉一个最高分和一个最...
用二维数组编程序:歌曲大赛,有M个评委给N个选手打分,求每个选手的平均得分(去掉一个最高分和一个最低分),并将各选手成绩由高到低排序.

人气:365 ℃ 时间:2025-06-09 12:21:03
解答

int main()
{
const unsigned int Ms = 4; // Ms must be greater than 2
const unsigned int Ns = 3;
double scores[Ns][Ms] = {{6.7, 8.5, 9.0, 4.6}, {6.7, 8.9, 8.9, 5.3}, {9.8, 9.6, 8.5, 4.3}};
double avgScores[Ns] = {};
for (int i = 0; i < Ns; i++)
{
double totalScore = scores[i][0];
for (int j = 1; j < Ms; j++)
{
totalScore += scores[i][j];
}
totalScore -= *std::min_element(scores[i], scores[i] + Ms);
totalScore -= *std::max_element(scores[i], scores[i] + Ms);
avgScores[i] = totalScore / (Ms - 2);
}
std::sort(avgScores, avgScores + Ns, std::greater());
for (int i = 0; i < Ns; i++)
{
cout

推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版