1. 案例描述
评委给5个人打分,去掉一个最高分和一个最低分后计算平均分,平均分为选手的最终得分。
2. 代码实现
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
class Person
{
public:
Person(string name, int score)
{
this->m_name = name;
this->m_score = score;
}
string m_name;
int m_score;
};
void set_score(vector<Person>&v)
{
for(vector<Person>::iterator it=v.begin(); it != v.end(); it++)
{
deque<int>d;
for(int i=0; i < 10; i++)
{
int score = rand() % 41 + 60;
d.push_back(score);
}
sort(d.begin(), d.end());
d.pop_back();
d.pop_front();
int sum = 0;
for(int i=0; i<d.size(); i++)
{
sum += d[i];
}
int avg = sum / 8;
(*it).m_score = avg;
}
}
void info_show(vector<Person>&v)
{
for(int i=0; i < v.size(); i++)
{
cout << v[i].m_name << ":" << v[i].m_score << endl;
}
}
int main()
{
string name_seed = "ABCDE";
vector<Person>v;
for(int i=0; i < 5; i++)
{
string name = "选手-";
name += name_seed[i];
int score = 0;
Person p(name, score);
v.push_back(p);
}
set_score(v);
info_show(v);
return 0;
}