十个评委给五个选手打分,去掉最高分和最低分,取平均分。
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
#include<ctime>
using namespace std;
class Person
{
public:
string m_Name;
int m_Score;
Person(string name, int score)
{
this->m_Name = name;
this->m_Score = score;
}
};
void CreatePerson(vector<Person>&v)
{
string strSeed = "ABCDE";
for (int i=0;i<5;i++)
{
string name = "选手";
name += strSeed[i];
int score = 0;
Person p(name, score);
v.push_back(p); // 将创建的Person放入到容器中
}
}
// 设置分数
void SetScore(vector<Person>&p)
{
for (vector<Person>::iterator it = p.begin(); it != p.end(); it++)
{
deque<int> d; //将评委的打分放在一个deque容器中
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;
d.push_back(score);
}
sort(d.begin(), d.end()); //指定要排序的元素的区间
// 去掉最高分和最低分
d.pop_front();
d.pop_back();
// 求平均分
int sum = 0;
for (deque<int>::iterator de = d.begin(); de != d.end(); de++)
{
sum += (*de);
}
int avg = sum / d.size();
it->m_Score = avg;
}
}
void PrintValue(const vector<Person>&p)
{
for (vector<Person>::const_iterator it = p.begin(); it != p.end(); it++)
{
cout << (*it).m_Name << " ; " << (*it).m_Score << endl;
}
}
int main()
{
// 添加一个随机数种子,否则每次产生的随机数,都和第一次的相同。
srand((unsigned int)time(NULL));
vector<Person>p;
CreatePerson(p);
SetScore(p);
PrintValue(p);
cin.get();
}