问题:
/*
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
//1. 创建五名选手,放到vector中
//2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
//3. sort算法对deque容器中分数排序,pop_back pop_front去除最高和最低分
//4. deque容器遍历一遍,累加分数,累加分数/d.size()
//5. person.score = 平均分
*/
在这里插入代#define _CRT_SECURE_N0_WARNINGS
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
using namespace std;
class Person
{
public:
string name;
int score;
Person(string name,int score)
{
this->name=name;
this->score=score;
}
};
void creatPerson(vector<Person>&v)
{
string nameSeed="ABCDE";
for (int i=0;i<5;i++)
{
string name=" ";
name+=nameSeed[i];
int score=0;
Person p(name,score);//创建临时对象
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;
d.push_back(score);
}
//去掉一个最高分一个最低分
sort(d.begin(),d.end());
d.pop_front();
d.pop_back();
//总分
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 showscore(vector<Person>&v)
{
for (vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
cout<<" 名字: "<<it->name<<" 平均分: "<<it->score<<endl;
}
}
int main()
{
//创建容器
vector<Person>v;
//创建5名选手
creatPerson(v);
//给5名选手打分
setscore(v);
//展示平均分
showscore(v);
system("pause");
return EXIT_SUCCESS;
}