1.案例——评委打分
案例描述
有五名选手,选手ABCDE,10个评委分别对每一个选手打分,去除最高分和最低分,取平均分。
#include<iostream>
#include<deque>
#include<vector>
#include<algorithm>
using namespace std;
class Person{
public:
string name;
int score;
Person(string name,int score):name(name),score(score){}
};
void setPerson(vector<Person>&v){
string base_name = "ABCDE";
for(int i=0;i<base_name.size();i++){
string name = "选手"+string(1,base_name[i]);
Person p(name, 0);
v.push_back(p);
}
}
void setScore(vector<Person>&v){
//如果不加引用,x只是v的副本
for(auto &x:v){
deque<int>q;
for(int i=1;i<=10;i++){
int score = rand()%41 + 60;
q.push_back(score);
}
sort(q.begin(),q.end());
q.pop_front();
q.pop_back();
int res = 0;
for(auto c:q){
res+=c;
}
cout<<res/q.size()<<endl;
x.score = res/q.size();
}
}
void PrintScore(vector<Person>&v){
for(auto x:v){
cout<<x.name<<" "<<x.score<<endl;
}
}
int main(){
vector<Person>v;
setPerson(v);
setScore(v);
PrintScore(v);
return 0;
}
2.案例——员工分组
案例描述
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作。
员工信息有:姓名、工资组成、部门分为:策划、美术、研发
通过multimap进行信息的插入key(部门编号)value(员工)分部门显示员工信息
#include<iostream>
#include<vector>
#include<map>
using namespace std;
#define cehua 0
#define donghua 1
#define itbumen 2
class worker{
public:
string name;
double salary;
worker(string name,double salary):name(name),salary(salary){}
};
void CreateWorker(vector<worker>&v){
string base_name="ABCDEF";
for(int i=0;i<base_name.size();i++){
string name = "员工"+string(1,base_name[i]);
double salary = rand()%10000 + 10000;
v.push_back(worker(name,salary));
}
}
void CreateGroup(vector<worker>&v,multimap<int,worker>&m){
for(auto x:v){
int id = rand()%3;
m.insert(make_pair(id,x));
}
cout<<"策划组:"<<endl;
auto it = m.find(cehua);
while(it!=m.end()&&it->first == cehua){
cout<<it->second.name<<" "<<it->second.salary<<endl;
it++;
}
cout<<"动画组:"<<endl;
it = m.find(donghua);
while(it!=m.end()&&it->first == donghua){
cout<<it->second.name<<" "<<it->second.salary<<endl;
it++;
}
cout<<"it组:"<<endl;
it = m.find(itbumen);
while(it!=m.end()&&it->first == itbumen){
cout<<it->second.name<<" "<<it->second.salary<<endl;
it++;
}
}
int main(){
vector<worker>v;
multimap<int,worker>m;
CreateWorker(v);
CreateGroup(v,m);
return 0;
}