比赛共24人,抽签决定分组,十个评委,去掉最高分以及最低分的平均分即为选手成绩,每次淘汰一半人,最后得出比赛前三名
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <deque>
#include<numeric>
#include <iostream>
using namespace std;
class Speaker
{
public:
string m_name;
int m_score[3];
};
int GetSpeaker(map<int,Speaker> &mapSpeaker,vector<int> &vec)
{
string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
random_shuffle(str.begin(),str.end());
for (int i=0;i<24;i++)
{
Speaker tem;
tem.m_name = "选手";
tem.m_name += str[i];
mapSpeaker.insert(make_pair(100+i,tem));
}
for (int i=0;i<24;i++)
{
vec.push_back(100+i);
}
return 0;
}
int speech_contest_draw(vector<int> & vec)
{
random_shuffle(vec.begin(),vec.end());
return 0;
}
int speech_contest(int index,map<int,Speaker>& mapSpeaker,vector<int> &vec1,vector<int> &vec2)
{
multimap<int,int,greater<int>> mapGroup;
int n = 0;
for (vector<int>::iterator it = vec1.begin();it!=vec1.end();it++)
{
n++;
deque<int> deScore;
for (int i=0;i<10;i++)
{
int score = 50 + rand() % 50;
deScore.push_back(score);
}
sort(deScore.begin(),deScore.end());
deScore.pop_back();
deScore.pop_front();//去除最低和最高
int scoreSum = accumulate(deScore.begin(),deScore.end(),0);
int scoreAvg = scoreSum / deScore.size();
mapSpeaker[*it].m_score[index] = scoreAvg;
mapGroup.insert(make_pair(scoreAvg,*it));
//处理分组
if (n % 6 == 0)
{
cout << "小组比赛成绩:" << endl;
for (multimap<int,int,greater<int>>::iterator it = mapGroup.begin();it!=mapGroup.end();it++)
{
cout << "编号:" << it->second << "\t" << "姓名:" << mapSpeaker[it->second].m_name << "\t" << "成绩:" << mapSpeaker[it->second].m_score[index] << endl;
}
while (mapGroup.size()>3)
{
multimap<int,int,greater<int>>::iterator it = mapGroup.begin();
vec2.push_back(it->second);
mapGroup.erase(it);
}
mapGroup.clear();
}
}
return 0;
}
int speech_contest_result(vector<int> vec,map<int,Speaker> mapSpeaker,int index)
{
cout << "第" << index + 1 << "轮比赛结果:" << endl;
for (vector<int>::iterator it = vec.begin();it!=vec.end();it++)
{
cout << "编号:" << *it << "\t" << "姓名:" << mapSpeaker[*it].m_name << "\t" << "成绩:" << mapSpeaker[*it].m_score[index] << endl;
}
return 0;
}
void main()
{
map<int,Speaker> mapSpeaker;
vector<int> vec1;//第一轮演讲比赛
vector<int> vec2;//第二轮演讲比赛
vector<int> vec3;//第三轮演讲比赛
vector<int> vec4;//第四轮演讲比赛
//获取比赛人员
GetSpeaker(mapSpeaker,vec1);
//第一轮比赛 抽签 比赛 查看结果
speech_contest_draw(vec1);
speech_contest(0,mapSpeaker,vec1,vec2);
speech_contest_result(vec2,mapSpeaker,0);
//第二轮比赛 抽签 比赛 查看结果
speech_contest_draw(vec2);
speech_contest(1,mapSpeaker,vec2,vec3);
speech_contest_result(vec3,mapSpeaker,1);
//第三轮比赛 抽签 比赛 查看结果
speech_contest_draw(vec3);
speech_contest(2,mapSpeaker,vec3,vec4);
speech_contest_result(vec4,mapSpeaker,2);
system("pause");
}