#include <iostream>
#include <vector>
#include <map>
#include <deque>
#include <algorithm>
#include <numeric>//算法求和文件
#include <unistd.h>
#include <string>
#include <ctime>
using namespace std;
class Speak{
public:
string name;
int score[3];
Speak() {
name = ""; // 或者你可以在这里给一个默认的名字
memset(score, 0, sizeof(score));
}
Speak(string name){
this->name = name;
memset(score,0,sizeof(score));
}
};
void cerateSpeak(vector<int> &v,map<int,Speak> &m)
{
string tmp = "ABCDEFGHIJKLMNOPQRSTUVWX";
for (int i = 0; i < tmp.size(); i++)
{
string name = "选手";
name += tmp[i];//选手名字
v.push_back(100+i);
m.insert(make_pair(100+i,Speak(name)));
}
}
void speechContest(int index,vector<int> v,map<int,Speak> &m,vector<int> &tmp)
{
multimap<int,int,greater<int> > mul;
cout<<"第" <<index <<"回合:" <<endl;
if(index > 1)
{
v.clear();
v.reserve(tmp.size());
copy(tmp.begin(),tmp.end(),v.begin());
}
tmp.clear();
random_shuffle(v.begin(),v.end());
int a = v.size()/6;
int count = 0;
while(a)
{
for (auto j = v.begin()+(count*6);j != v.begin()+(count+1)*6 && j!=v.end();j++)
{
//生成10个分数
deque<int> d;
for (int i = 0; i < 10; i++)
{
d.push_back(rand()%41+60);
}
//排序
sort(d.begin(),d.end());
//去掉最高分和最低分
d.pop_back();
d.pop_back();
int sum = accumulate(d.begin(),d.end(),0);
m[*j].score[index-1] = sum/d.size();
mul.insert(make_pair(m[*j].score[index-1],*j));
}
int num = 0;
// tmp.clear();
for (auto m = mul.begin();m != mul.end() && num <3; m++,num++)
{
tmp.push_back((*m).second);
}
count++;
a--;
}
}
int main()
{
srand(time(NULL));
map<int,Speak> m;//选手编号,选手信息
vector<int> v;//选手编号
cerateSpeak(v,m);
for (int i = 1; i < 4; i++)
{
vector<int> tmp;//临时容器用来保存晋级之后的选手编号
speechContest(i,v,m,tmp);
for(auto a:tmp)
{
cout<<"name:"<<m[a].name <<",第"<<i <<"轮分数为:"<<m[a].score[i-1] <<endl;
}
}
return 0;
}
copy(tmp.begin(), tmp.end(), v.begin());
这行代码没有生效
运行后,v直接空了
输入
10 3
**输出 **
3 6 9 2 7 1 8 5 10 4