目录
1--评委打分
1-1--案例描述
1-2--代码实现
2--员工分组
2-1--案例分析
2-2--代码实现
1--评委打分
1-1--案例描述
有 A, B, C, D, E 共 5 名选手,由 10 个评委分别对每一名选手打分,去除评委的最高分和最低分,取剩下 8 名评委的平均分作为最终选手的评分;
1-2--代码实现
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
class Person{
public:
Person(std::string name, int score){
this -> name = name;
this -> score = score;
}
std::string name;
int score;
};
void createPerson(std::vector<Person>& v){
std::string nameSeed = "ABCDE";
for(int i = 0; i < 5; i++){
std::string name = "player";
name += nameSeed[i];
int score = 0;
Person p(name, score);
v.push_back(p);
}
}
void setScore(std::vector<Person>&v){
for(std::vector<Person>::iterator it = v.begin(); it != v.end(); it++){
// 将评委的分数放入到deque容器中
std::deque<int>d;
for(int i = 0; i < 10; i++){
int score = rand()%41 + 60; // 60 -100
d.push_back(score);
}
std::cout<< "player: " << (*it).name << ", score: " << std::endl;
for(std::deque<int>::iterator it = d.begin(); it != d.end(); it++){
std::cout << *it << " ";
}
std::cout << std::endl;
// 排序
std::sort(d.begin(), d.end());
// 去除最高分和最低分
d.pop_back();
d.pop_front();
// 取平均分
int sum = 0;
for(std::deque<int>::iterator dit = d.begin(); dit != d.end(); dit++){
sum += *dit;
}
int avg = sum / d.size();
it->score = avg;
}
}
void showScore(std::vector<Person> &v){
std::cout << "Final Score: " << std::endl;
for(std::vector<Person>::iterator it = v.begin(); it != v.end(); it++){
std::cout << "name: " << (*it).name << ", socre: " << (*it).score << std::endl;
}
}
int main(int argc, char* argv[]){
srand( (unsigned int)time(NULL));
// 创建 5 名选手
std::vector<Person>v;
createPerson(v);
// 给选手打分
setScore(v);
// 显示最终得分
showScore(v);
return 0;
}
2--员工分组
2-1--案例分析
公司招聘 10 个员工(ABCDEFGHIJ),员工信息包括姓名和工资,需要将员工分配到策划、美术和研发三个部门;通过 multimap 容器进行员工信息的插入,并分部分显示员工的信息;
2-2--代码实现
#include "iostream"
#include "string"
#include "vector"
#include "map"
#include "ctime"
class Person{
public:
std::string Name;
int Salary;
};
void CreateWorker(std::vector<Person> &v){
std::string nameseed = "ABCDEFGHIJ";
for(int i = 0; i < 10; i++){
Person p;
p.Name = "Worker";
p.Name += nameseed[i];
p.Salary = rand() % 10000 + 10000; // 10000 ~ 19999
v.push_back(p);
}
}
void SetGroup(std::vector<Person> &v, std::multimap<int, Person> &m){
for(std::vector<Person>::iterator it = v.begin(); it != v.end(); it++){
int ID = rand() % 3; // 0, 1, 2
m.insert(std::make_pair(ID, *it));
}
}
void ShowWorkerByGroup(std::multimap<int, Person> &m){
std::cout << "Group of design: " << std::endl;
std::multimap<int, Person>::iterator pos = m.find(0);
int count = m.count(0);
for(int i = 0; pos != m.end() && i < count; pos++, i++){
std::cout << "Group: " << pos->first << " Name: " << pos->second.Name << " Salary: " << pos->second.Salary << std::endl;
}
std::cout << "Group of Art: " << std::endl;
pos = m.find(1);
count = m.count(1);
for(int i = 0; pos != m.end() && i < count; pos++, i++){
std::cout << "Group: " << pos->first << " Name: " << pos->second.Name << " Salary: " << pos->second.Salary << std::endl;
}
std::cout << "Group of research: " << std::endl;
pos = m.find(2);
count = m.count(2);
for(int i = 0; pos != m.end() && i < count; pos++, i++){
std::cout << "Group: " << pos->first << " Name: " << pos->second.Name << " Salary: " << pos->second.Salary << std::endl;
}
}
int main(int argc, char* argv[]){
srand((unsigned int) time(NULL));
std::vector<Person> v;
CreateWorker(v);
std::multimap<int, Person> m;
SetGroup(v, m);
ShowWorkerByGroup(m);
return 0;
}