#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<deque>
#include<numeric>
#include<functional>
using namespace std;
/*
1) 产生选手 ( ABCDEFGHIJKLMNOPQRSTUVWX ) 姓名、得分;选手编号
2) 第1轮 选手抽签 选手比赛 查看比赛结果
3) 第2轮 选手抽签 选手比赛 查看比赛结果
4) 第3轮 选手抽签 选手比赛 查看比赛结果
*/
class Speaker {
public:
string name;
int m_scores[3];
};
void CreatSpeaker(vector<int>&v, map<int, Speaker>&m){
string nameSeed = "ABCDEFGHIJKLMNOPQRSTUVWX";
for (int i = 0; i < nameSeed.size(); i++) {
string name_tmp = "PLAYER-";
name_tmp += nameSeed[i];
Speaker sp;
sp.name = name_tmp;
for (int j = 0; j < 3; j++) {
sp.m_scores[j] = 0;
}
v.push_back(i+100);
m.insert(make_pair(i+100,sp));
}
}
//抽签
void SpeechDraw(vector<int>&v) {
//洗牌
random_shuffle(v.begin(),v.end());
}
//参数1 第几轮 ,参数2 选手编号,参数3 选手编号与具体选手,参数4 晋级选手编号容器
void SpeechDraw(int index ,vector<int> &v1, map<int, Speaker>&m, vector<int> &v2) {
multimap<int, int, greater<int>> GroupMap;//key 分数 val 编号
int num = 0;
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) {
num++;
deque<int> d_tmp;
for (int i = 0; i < 10; i++) {
int Scores_tmp = rand() % 41 + 60;
d_tmp.push_back(Scores_tmp);
}
//排序
sort(d_tmp.begin(), d_tmp.end());
d_tmp.pop_back();
d_tmp.pop_front();
int sum = accumulate(d_tmp.begin(), d_tmp.end(), 0);
int avg_tmp = sum / d_tmp.size();
//将平均分放入容器中
m[*it].m_scores[index-1] = avg_tmp;
//每6个人 取前三名晋级
//临时容器 保存6人
//临时容器 存放数据
GroupMap.insert(make_pair(avg_tmp,*it));
if (num % 6 == 0) {
cout << "小组比赛成绩如下:" << endl;
for (multimap<int, int, greater<int>>::iterator mit1 = GroupMap.begin(); mit1 != GroupMap.end();mit1++) {
cout << " 编号:" << mit1->second
<< ",姓名:" << m[mit1->second].name
<< ",得分:" << m[mit1->second].m_scores[index - 1] <<" ."<< endl;
}
cout << endl;
int count_tmp = 0;
for (multimap<int, int, greater<int>>::iterator mit2 = GroupMap.begin(); mit2 != GroupMap.end(),count_tmp<3; mit2++, count_tmp++) {
v2.push_back(mit2->second);
}
GroupMap.clear();//清空临时容器
}
}
}
void ShowScore(int index, vector<int>&v, map<int, Speaker>&m) {
cout << "第 " << index << " 轮比赛成绩如下:" << endl;
for (map<int, Speaker>::iterator it = m.begin(); it != m.end(); it++) {
cout << " 编号:" << it->first
<< ",姓名:" << it->second.name
<< ",得分:" << it->second.m_scores[index-1] <<" ."<< endl;
}
cout << "晋级选手编号:" ;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << ", ";
}
cout << endl;
cout << endl;
}
int main() {
vector<int>v1;//player's number
map<int, Speaker>m1;//player's number and name
CreatSpeaker(v1,m1);
//test
//for (map<int, Speaker>::iterator it = m1.begin(); it != m1.end(); it++) {
// cout << "编号:" << it->first << ",姓名:" << it->second.name << endl;
//}
//第一轮比赛
//抽签
SpeechDraw(v1);
//比赛
vector<int>v2;//进入下一轮人员编号
SpeechDraw(1,v1,m1,v2);
//显示比赛结果
ShowScore(1, v2, m1);
//第二轮比赛
SpeechDraw(v2);
vector<int>v3;//进入下一轮人员编号
SpeechDraw(2, v2, m1, v3);
//显示比赛结果
ShowScore(2, v3, m1);
//第三轮比赛
SpeechDraw(v3);
vector<int>v4;//进入下一轮人员编号
SpeechDraw(3, v3, m1, v4);
//显示比赛结果
ShowScore(3, v4, m1);
system("pause");
return EXIT_SUCCESS;
}
我的结果如下: