#include<iostream>
//#include<stdlib.h>
#include<cstdlib> // 效果同上
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
#include<ctime>
/*
3.4 案例-评委打分
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分最低分,取平均分。
1. 创建五名选手,放到vector中
2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
3. sort算法对deque容器中分数排序,去除最高和最低分
4. deque容器遍历一遍,累加总分
5. 获取平均分
*/
class Person
{
public:
string name;
int score;
public:
Person(string _name, int _score)
{
this->name = _name;
this->score = _score;
}
};
void create_person(vector<Person> &v)
{
string name_seed = "ABCDE";
for(int i=0; i<5; i++)
{
string name = "选手";
name += name_seed[i];
int score = 0;
Person p(name, score);
v.push_back(p);
}
}
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; //60~100; rand()%40:0~39
d.push_back(score);
}
/*
cout << "name:" << it->name << " score:" << endl;
for(deque<int>::iterator dit=d.begin(); dit!=d.end(); dit++)
{
cout << *dit << " ";
}
cout << endl;
*/
sort(d.begin(), d.end()); //排序,默认升序
d.pop_back(); //去除最高分
d.pop_front(); //去除最低分
int sum = 0;
for(deque<int>::iterator dit=d.begin(); dit!=d.end(); dit++)
{
sum += *dit;
}
int avg = sum / d.size(); // 求平均分
it->score = avg;
}
}
void show_score(vector<Person> &v)
{
for(vector<Person>::iterator it=v.begin(); it!=v.end(); it++)
{
cout << "name:" << (*it).name << " score:" << (*it).score << endl;
}
}
int main()
{
srand((unsigned int)time(NULL)); //真随机:随机数种子;防止每次运行打分都一样
vector<Person> v;
create_person(v);
/*
for(vector<Person>::iterator it=v.begin(); it!=v.end(); it++)
{
cout << "name:" << (*it).name << " score:" << (*it).score << endl;
}
*/
set_score(v);
show_score(v);
system("pause");
return 0;
}

原文:https://www.cnblogs.com/yppah/p/14752804.html