STL simulation speech contest

        A speech contest (speech_contest) was held in a certain city. A total of 24 people participated. The contest had three rounds. The first two rounds were knockout rounds and the third round was the final.
        Competition method: group competition, each group of 6 people, each time the contestants are randomly divided into groups, to compete; the
        first round is divided into 4 groups, each group of 6 people, for example, the number is 100~123, the overall draw (draw) after sequential speech .
        When the group has finished speaking, eliminate the last three players in the group, and then continue to the next group competition.
        The second round is divided into 2 groups with 6 people in each group. At the end of the game, the last three players in the group are eliminated, and then the next group competition will continue.
        In the third round, there are only one group and six people left. This round is the final and the top three are selected.
        Competition score: 10 judges score, remove the lowest score and the highest score, and find the average score.
        After each contestant has finished speaking, 10 judges will score points respectively, and the average score is the final score of the contestant.
        The players are ranked in descending order of score.
        The STL simulation is as follows:

#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <map>
#include <deque>
#include <numeric>
#include <ctime>
using namespace std;

/*
   某市举行一场演讲比赛(speech_contest),共有24个人参加,比赛共三轮,前两轮为淘汰赛,
第三轮为决赛。
   比赛方式:分组比赛,每组6个人,选手每次要随机分组,进行比赛;
   第一轮分为4组,每组6个人,比如编号为100~123,整体进行抽签(draw)后顺序演讲。
当小组演讲完后,淘汰组内排名最后的三个选手,然后继续下一个小组的比赛。
   第二轮分为2组,每组6个人。比赛完毕,淘汰组内排名最后的三个选手,然后继续下一个小组的比赛。
   第三轮只剩1组,6个人,本轮为决赛,选出前3名。
   比赛评分:10个评委打分,去除一个最低分、一个最高分,求平均分。
   每个选手演讲完后,由10个评委分别打分,平均分是选手的最终得分。
   选手的名次按得分降序排列。

*/


/*
//1) 产生选手(ABCDEFGHIJKLMNOPQRSTUVWX) 姓名、得分、选手编号
//2) 第1轮 选手抽签 选手比赛 查看比赛结果 
//3) 第2轮 选手抽签 选手比赛 查看比赛结果
//4) 第3轮 选手抽签 选手比赛 查看比赛结果

*/

class Speaker
{
    
    
public:


	string m_Name; //姓名
	int m_Score[3]; //得分数组

};

void createSpeaker(vector<int> &v, map<int,Speaker> &m)
{
    
    
	string nameSeed = "ABCDEFGHIJKLMNOPQRSTUVWX";
	for (int i = 0; i < nameSeed.size();i++)
	{
    
    
		string name = "选手";
		name += nameSeed[i];

		Speaker sp;
		sp.m_Name = name;
		for (int j = 0; j < 3;j++)
		{
    
    
			sp.m_Score[j] = 0;
		}

		v.push_back(i + 100); //选手编号100~123
		m.insert(make_pair(i + 100, sp));


	}


}

void speechDraw(vector<int> &v)
{
    
    
	//洗牌
	random_shuffle(v.begin(), v.end());
}

//index代表第几轮
//v1为比赛选手
//m为选手编号和具体选手
//v2为晋级选手
void speechContest(int index,vector<int> &v1,map<int,Speaker> &m, vector<int> &v2)
{
    
    
	multimap<int, int, greater<int>> groupMap; //key 分数, value 编号
	int num = 0;

	for (vector<int>::iterator it = v1.begin(); it != v1.end();it++)
	{
    
    
		num++;
		deque<int> d;
		for (int i = 0; i < 10;i++)
		{
    
    
			int score = rand() % 41 + 60; //60~100
			d.push_back(score);
		}
		//排序
		sort(d.begin(), d.end());
		d.pop_back();
		d.pop_front();
		//累计分数
		int sum = accumulate(d.begin(), d.end(), 0);
		int avg = sum / d.size();

		//将平均分放入score容器中
		m[*it].m_Score[index - 1] = avg;

		//每6个人取前3名晋级
		//临时容器保存排名
		//临时容器存入数据
		groupMap.insert(make_pair(avg, *it));
		if (num % 6 == 0)
		{
    
    
			//cout << "小组比赛成绩: " << endl;
			//for (multimap<int, int, greater<int>>::iterator mit = groupMap.begin();
			//	mit != groupMap.end(); mit++)
			//{
    
    
			//	cout << "编号: " << mit->second << ",姓名:" << m[mit->second].m_Name
			//		<< ",得分:" << m[mit->second].m_Score[index - 1] << endl;
			//}

			//取前三名
			int count = 0;
			for (multimap<int, int, greater<int>>::iterator mit = groupMap.begin();
				mit != groupMap.end(),count<3; mit++,count++)
			{
    
    
				v2.push_back(mit->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.m_Name
			<<" 分数:"<<it->second.m_Score[index-1]<< endl;
	}


	cout << "晋级选手编号: " << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end();it++)
	{
    
    
		cout << *it << endl;
	}

}



int main()
{
    
    
	//随机种子
	srand((unsigned int)time(NULL));

	vector<int> v1; //选手编号

	map<int, Speaker> m; //存放选手编号和对应的具体选手

	//创建选手
	createSpeaker(v1,m);

	//抽签
	speechDraw(v1);

	vector<int> v2; //进入下一轮比赛的人员编号

	//比赛
	speechContest(1, v1, m, v2);

	//显示比赛结果
	showScore(1, v2, m);//轮数 晋级信息 选手

	//第二轮比赛
	speechDraw(v2);
	vector<int> v3;
	speechContest(2, v2, m,v3);

	showScore(2, v3, m);

	//第三轮比赛
	speechDraw(v3);
	vector<int> v4;
	speechContest(3, v3, m, v4);
	showScore(3, v4, m);


	//测试
	//for (map<int, Speaker>::iterator it = m.begin(); it != m.end();it++)
	//{
    
    
	//	cout << "编号: " << it->first << ", 姓名: " << it->second.m_Name << endl;
	//}


	system("pause");
	return 0;
}

        The effect is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/sanqima/article/details/103840374