1、评委打分
1.1 描述
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
1.2 实现
-
创建五名选手,放到vector中
-
遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
-
sort算法对deque容器中分数排序,去除最高和最低分
-
deque容器遍历一遍,累加总分
-
获取平均分
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <string>
#include <ctime>
using namespace std;
/*
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,
去除最高分,去除评委中最低分,取平均分。
*/
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)
{
string nameSeed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手";
name = name + nameSeed[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容器
deque<int>d;
for (int i = 0; i < 10; i++)
{
int score = rand() % 40 + 60; //60 - 99
d.push_back(score);
}
cout << "选手:" << it->m_Name << " 打分:";
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 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(void)
{
//随机数的种子
srand((unsigned int)time(NULL));
//1、创建5名选手
vector<Person>v;
createPerson(v);
//测试
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout <<"姓名:" <<(*it).m_Name << " 分数:"<<(*it).m_Score << endl;
}
//2、给选手打分
setScore(v);
//3、显示最后得分
showScore(v);
system("pause");
return 0;
}
总结:用到vector、deque、string容器和排序算法
2、员工分组
2.1 描述
-
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
-
员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
-
随机给10名员工分配部门和工资
-
通过multimap进行信息的插入 key(部门编号) value(员工)
-
分部门显示员工信
2.2 实现
-
创建10名员工,放到vector中
-
遍历vector容器,取出每个员工,进行随机分组
-
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
-
分部门显示员工信息
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <ctime>
using namespace std;
/*
* 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
* 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
* 随机给10名员工分配部门和工资
* 通过multimap进行信息的插入 key(部门编号) value(员工)
* 分部门显示员工信息
*/
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
//员工类
class Worker
{
public:
string m_Name;
int m_Salary;
};
//创建员工
void createWorker(vector<Worker> &v) //引用或者指针
{
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i]; //拼接
worker.m_Salary = rand() % 10000 + 10000; //10000-19999
v.push_back(worker); //员工加入到容器中
}
}
//分组
void setGroup(vector<Worker>&v, multimap<int, Worker> &m)
{
//变量vector容器
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机的部门的编号【3个部门】
int deptId = rand() % 3; //0,1,2
//将员工插入到分组中
m.insert(make_pair(deptId, *it)); //key是部门编号 value是员工信息
}
}
//分组显示
void showWorkerByGroup(multimap<int, Worker>&m)
{
cout << "策划部门的员工:" << endl;
multimap<int,Worker>::iterator pos = m.find(CEHUA); //策划部门的起始位置,因为key升序
int count = m.count(CEHUA); //统计策划部门人数
int index = 0;
for (; pos != m.end()&&index<count ; pos++,index++)
{
cout << "姓名:" << pos->second.m_Name << " 年龄:" << pos->second.m_Salary << endl;
}
cout << "----------------------------------------------------" << endl;
cout << "美术部门的员工:" << endl;
pos = m.find(MEISHU); //策划部门的起始位置,因为key升序
count = m.count(MEISHU); //统计策划部门人数
index = 0;
for (; pos != m.end() && index<count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << " 年龄:" << pos->second.m_Salary << endl;
}
cout << "----------------------------------------------------" << endl;
cout << "研发部门的员工:" << endl;
pos = m.find(YANFA); //策划部门的起始位置,因为key升序
count = m.count(YANFA); //统计策划部门人数
index = 0;
for (; pos != m.end() && index<count; pos++, index++)
{
cout << "姓名:" << pos->second.m_Name << " 年龄:" << pos->second.m_Salary << endl;
}
}
int main(int argc, int* argv[])
{
//随机数种子
srand((unsigned int)time(NULL));
//1、创建员工
vector<Worker>vWorker;
createWorker(vWorker);
//测试
/*
for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
{
cout <<"姓名:" <<it->m_Name << " 工资:" << it->m_Salary << endl;
}
*/
//2、员工分组
multimap<int, Worker>mWorker; //用multimap,key是部门,value是员工,有可能多个员工在同一个部门
setGroup(vWorker, mWorker); //将员工 和 分组的部门传进去
//分组显示员工
showWorkerByGroup(mWorker);
system("pause");
return 0;
}
总结:用到vector、multimap、string容器