//vector容器嵌套deque容器
//因为需要对数据头部(两端)操作,故采用queue容器表示选手所打分数
#include<iostream>
using namespace std;
#include<vector>
#include<deque>
#include<algorithm>
#include<ctime>
//视频思路:将评委的十个打分与平均分分开考虑,平均分设置为person类,而评委打分为deque容器储存
//最后让queue遍历后计算的数值等于person类的平均值
//用vector容器储存person类,就像一个储存person类的数组
class person {
public:
person(string name, int score) {
m_name = name;
m_score = score;
}
string m_name;
int m_score;
};
//选手创建
void createperson(vector<person>& v) {
//容器的person对象不可以直接引用赋值
//v.m_score = 0;
string namespeed = "ABCDE";
for (int i = 0; i < 5; i++) {
string name = "选手";
name += namespeed[i];
int score = 0;
person p(name, score);
//将创建好的person对象放入到容器内
v.push_back(p);
}
}
//选手打分
void setscore(vector<person>& v) {
//在这里声明deque,需要clear清空
//deque<int>d;
for (vector<person>::iterator it = v.begin(); it != v.end(); it++) {
deque<int>d;
cout << "选手: ";
for (int i = 0; i < 10; i++) {
int score = rand() % 41 + 60; //60-100 如果除40,rand()%40范围为0-39
cout << score << " ";
d.push_back(score);
}
//排序
sort(d.begin(), d.end());
//去除最高最低分
d.pop_back();
d.pop_front();
cout << endl;
//取平均分
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) {
sum += *dit;
}
int avg = sum / 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));
//1.创建五名选手
vector<person>v; //存放选手容器
createperson(v);
//2.给五名选手打分
setscore(v);
//3.显示
showscore(v);
}
|