案例描述
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分
实现步骤
1.创建五名选手,放到vector中
2.遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
3.sot算法对deque容器中分数排序,去除最高和最低分
4.deque容器遍历一遍,累加总分
5.获取平均分
代码实现
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>using namespace std;class person
{
public:person(string name,int score){this->m_name = name;this->m_score = score;}int m_score;//平均分string m_name;//姓名
};void createperson(vector<person>&v)
{string s = "ABCDE";for(int i = 0;i < 5;i++){string name = "选手";name += s[i]; int score = 0;person p(name,score);//将创建的person对象 放入到容器中v.push_back(p);}
}
//分数
void setscore(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 d.push_back(score);}cout << "姓名:" << it->m_name<< " 得分:" << 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 num = 0;for(deque<int>::iterator dit = d.begin();dit != d.end();dit++){num += *dit;}int avg = num / d.size();it->m_score = avg;}
}void showscore(vector<person>&v)
{for(vector<person>::iterator it = v.begin();it != v.end();it++){cout << "姓名: "<<it->m_name << " 平均分:"<< it->m_score<< endl;}
}int main()
{//随机数种子srand((unsigned int)time(NULL));//创建选手vector <person> v;//创建一个存放person类型的voctor容器createperson(v);//测试 迭代器指向v头部 /*for(vector<person>::iterator it = v.begin();it != v.end();it++){cout << "姓名: "<< (*it).m_name<<"成绩:" <<(*it).m_score << endl;}*/setscore(v);cout<< endl;showscore(v);return 0;
}
编译运行