#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
#include<deque>
#include<ctime>
class Person
{
public:
Person(string na, int s) :name(na), score(s) {}
string name;
int score;
};
void PrintPerson(vector<Person>&v) //输出姓名和平均分
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)//遍历vector容器
{
cout << (*it).name << " " << (*it).score << endl;
}
}
void Setscore(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
deque<int>d; //创建一个deque容器存放十个数
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;//随机产生数字
d.push_back(score);//将数字放入deque容器中
}
sort(d.begin(), d.end());//对deque容器进行从小到大的排序
d.pop_back(), d.pop_front();//除去最大的和最小的两个数
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)//遍历deque容器求8个数字的和
{
sum +=*dit;
}
(*it).score = sum / d.size();//将平均值赋给vector容器中每个Person的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);
}
}
int main()
{
srand((unsigned int)time(NULL));
vector<Person> v;
creatPerson(v);
Setscore(v);
PrintPerson(v);
return 0;
}
deque和vector容器评委打分项目
最新推荐文章于 2024-07-13 19:27:27 发布