3.4 案例-评委打分
3.4.1 案例描述
有5名选手: 选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分
3.4.2 实现步骤
- 创建五名选手,放到vector中
- 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
- sort算法对deque容器中分数排序,去除最高和最低分
- deque容器遍历一遍,累加总分
- 获取平均分
#include <iostream>
#include<vector>
#include<algorithm>//标准算法的头文件
#include<string>
#include<deque>
#include<ctime>
using namespace std;
class Xuanshou
{
public:
Xuanshou(string name, int score)
{
this->m_name = name;
this->m_score = score;
}
string m_name;
int m_score;
};
void Create_5_Xuanshou(vector<Xuanshou>&v)
{
string str = "ABCDE";
for (int i = 0; i < 5; i++)
{
string str2 = "选手";
str2 += str[i];
int score = 0;
Xuanshou xuan(str2, score);
v.push_back(xuan);
}
}
void Dafen(vector<Xuanshou> &v)
{
for (vector<Xuanshou>::iterator it = v.begin(); it != v.end(); it++)
{
//cout << (*it).m_name << " " << (*it).m_score << endl;
deque<int> d;
cout <<"对"<< (*it).m_name << "打分!" << endl;
for (int j = 0; j < 10; j++)
{
cout << "评委"<<j+1<<"打分!" << endl;
int setScore = rand() % 41 + 60;//60~100分
d.push_back(setScore);
}
sort(d.begin(), d.end());
d.pop_back();
d.pop_front();
int sum = 0;
/*
for (int i = 0; i < d.size(); i++)
{
sum += d.at(i);
}
*/
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
sum += (*it);
}
int avg = sum / d.size();
it->m_score = avg;
}
}
int main()
{
srand((unsigned int)time(NULL));
//test();
//test02();
//test03();
//test04();
//test05();
//test000();
//test001();
//test002();
vector<Xuanshou> v;
Create_5_Xuanshou(v);
Dafen(v);
for (vector<Xuanshou>::iterator it = v.begin(); it != v.end(); it++)
{
cout << (*it).m_name << " " << (*it).m_score << endl;
}
std::cout << "Hello World!\n";
}