有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低 分,取平均分
- 创建五名选手,放到vector中
- 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容 器中
- sort算法对deque容器中分数排序,pop_back pop_front去除最高和最低分
- deque容器遍历一遍,累加分数,累加分数/d.size()
- person.score = 平均分
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <time.h>
using namespace std;
class player
{
public:
player(int score,string name)
{
this->name = name;
this->score = score;
}
void Showme()
{
cout<<name<<" "<<score<<endl;
}
int score;
string name;
};
void init_player(vector<player>& v)
{
string NameJi = "ABCDE";
for(int i=0;i<5;i++)
{
player tmp(0,string(1,NameJi[i]));
v.push_back(tmp);
}
}
void start_player(vector<player>& v)
{
srand((unsigned int)time(NULL));
deque<int> d;
for(vector<player>::iterator it = v.begin();it != v.end();it++)
{
for(int i=0;i<10;i++)
{
d.push_back(60 + rand() % 41);
}
sort(d.begin(), d.end());
d.pop_back();
d.pop_front();
int sum = 0;
for (deque<int>::iterator it1 = d.begin(); it1 != d.end(); it1++)
{
sum += *it1;
}
sum/=d.size();
it->score = sum;
d.clear();
}
}
void print(player& p)
{
p.Showme();
}
int main()
{
vector<player> v;
init_player(v);
for_each(v.begin(),v.end(),print);
start_player(v);
for_each(v.begin(),v.end(),print);
return 0;
}