3.4 案例-评委打分
3.4.1 案例描述
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
3.4.2 实现步骤
- 创建五名选手,放到vector中
- 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
- sort算法对deque容器中分数排序,去除最高和最低分
- deque容器遍历一遍,累加总分
- 获取平均
#include<iostream>
using namespace std;
#include<deque>
#include<algorithm>
#include<vector>
#include<string>
#include<ctime>
class person
{
public:
person(string name,int score)
{
this->name = name;
this->sorce = score;
}
string name;
int sorce;
};
void creatperson(vector<person>&v)
{
/*person p1("A", 0);
person p2("B", 0);
person p3("C", 0);
person p4("D", 0);
person p5("E", 0);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);*/
string nameseed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手";
person p(name + nameseed[i], 0);
v.push_back(p);
}
}
void personscore(vector<person>&v)
{
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
//将评委分数 放入deque
deque<int>d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;
d.push_back(score);
}
//排序
sort(d.begin(), d.end());
//测试
/*for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
cout << *dit << " ";
}
cout << endl;*/
d.pop_back();
d.pop_front();
int all = 0;
/*for (deque<int>::iterator dit=d.begin();dit!=d.end();dit++)
{
all += *dit;
}*/
for (int i = 0; i < 8; i++)
{
all += d[i];
}
int avg = all / 8;
it->sorce = avg;
}
}
void show(vector<person>&v)
{
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名: " << (*it).name << " 分数: " << (*it).sorce << endl;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
//创建5名选手
vector<person>v;
creatperson(v);
测试
//for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << (*it).name << " " << (*it).sorce << endl;
//}
//给5名选手打分
personscore(v);
//显示最后的得分
show(v);
system("pause");
return 0;
}