(c + +) comprehensive case of STL school speech contest

Posted by konky on Sat, 06 Nov 2021 17:53:51 +0100

Problem description

1) A city held a speech_contest, in which 24 people participated. There are three rounds in the competition. The first two rounds are the knockout and the third round is the final.
2) Competition mode: group competition, with 6 people in each group; Players should be randomly divided into groups for competition each time;
            the first round was divided into 4 groups with 6 people in each group. For example, 100-105 is a group, 106-111 is the second group, and they are classified in turn
PUSH,
            each person will give a speech in the order of draw ing lots. After the group speech, eliminate the last three contestants in the group,
Then continue to the next group game.
           the second round is divided into two groups with 6 people in each group. After the competition, eliminate the last three players in the group, and then continue
The next group game.
There are only 6 people left in the third round. This round is the final and the top three are selected.
4) Competition score: 10 judges score, remove the lowest and highest scores, and get an average score
           after each contestant's speech, 10 judges will score respectively. The player's final score is to remove a highest score and a lowest score
Low score, get the average score of the remaining 8 scores.
                             820. STL programming is used to solve this problem
1) Please print out the names and numbers of all contestants and arrange them in ascending order.
2) Print the results of the group competition and the promotion list of the group after each round of competition
3) Print the names and scores of the top three finalists.

Realization idea

It is necessary to save player information, player score information, player competition lottery information and player promotion information in the container
In, the selection of each container needs to be involved. (equivalent to E-R diagram design of information database)
Players can design a class Speaker (name and score)
All player numbers and player information can be placed in the container: Map < int, speaker >
The number information of all contestants can be placed in the container: vecter v1
The first round promotion list can be placed in the container vecter v2
The second round promotion list can be placed in the container vecter v3
The list of the top three in the third round can be placed in the container vecter v4
The score of each player can be placed in the container deque dscore; Easy to remove the lowest and highest scores

Implementation details

void main()
{
//Define the data structure and put all players in the container
map<int, Speaker> mapSpeaker;vector<int> v1; //List of the first round of speech contest
vector<int> v2; //List of the second round of speech contest
vector<int> v3; //List of the third round of speech contest
vector<int> v4; //Final speech contest list
//Generate players
GenSpeaker(mapSpeaker, v1);
//Players in the first round draw lots to check the competition results (scores in the promotion list)
cout << "\n\n\n any key,Start the first round" << endl;
cin.get();
speech_contest_draw(v1);
speech_contest(0, v1, mapSpeaker, v2);
speech_contest_print(0, v2, mapSpeaker);
//The players in the second round draw lots to check the results
cout << "\n\n\n any key,Start the second round" << endl;
cin.get();
speech_contest_draw(v2);
speech_contest(1, v2, mapSpeaker, v3);
speech_contest_print(1, v3, mapSpeaker);
//In the third round, the players draw lots to check the results
cout << "\n\n\n any key,Start the third round" << endl;
cin.get();
speech_contest_draw(v3);
speech_contest(2, v3, mapSpeaker, v4);
speech_contest_print(2, v4, mapSpeaker);
system("pause");
}
//Generate players
int GenSpeaker(map<int, Speaker> &mapSpeaker, vector<int> &v1)
//Players draw lots
int speech_contest_draw(vector<int> &v)
//Player competition
int speech_contest(int index, vector<int> &v1, map<int, Speaker> &mapSpeaker, vector<int>
&v2)
//Print the promotion list of players
int speech_contest_print(int index, vector<int> v, map<int, Speaker> & mapSpeaker)

Source code.cpp

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<time.h>
#include<stdlib.h>
#include<deque>
#include<algorithm>
using namespace std;

class Speaker
{
public:
	Speaker()
	{
	}
	Speaker(string name,int score)
	{
		this->mName = name;
		this->mScore = score;
	}
	~Speaker()
	{

	}
	string mName;
	int    mScore;
private:
};

//Generate players
int GenSpeaker(map<int, Speaker>& mapSpeaker, vector<int>& v1)
{
	string seedName = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (int i = 0; i < 24; i++)
	{
		Speaker mspeaker;
		mspeaker.mName = "player";
		mspeaker.mName += seedName[i];

		mspeaker.mScore = 0;

		mapSpeaker.insert(make_pair(i + 1, mspeaker));
		v1.push_back(i + 1);
	}

	cout << "-----------List of contestants--------------" << endl;

	for (map<int, Speaker>::iterator it = mapSpeaker.begin(); it != mapSpeaker.end(); it++)
	{
		cout << "No.:" << (*it).first << "  " << "full name:" << (*it).second.mName << endl;
	}

	return 0;
}
//Players draw lots
int speech_contest_draw(vector<int>& v)
{
	srand(time(NULL));
	vector<int> vin = v;
	int temp = vin.size() / 6;
	if (vin.size() > 0)
	{
		multimap<int, int> SpeakerGroup;

		for (vector<int>::iterator it = vin.begin(); it != vin.end(); it++)
		{
			int Group = rand() % temp + 1;
			switch (Group)
			{
			case 1:
				if (SpeakerGroup.count(1) == 6)
				{
					it--;
					break;
				}
				else
				{
					SpeakerGroup.insert(make_pair(1, *it));
					break;
				}
			case 2:
				if (SpeakerGroup.count(2) == 6)
				{
					it--;
					break;
				}
				else
				{
					SpeakerGroup.insert(make_pair(2, *it));
					break;
				}
			case 3:
				if (SpeakerGroup.count(3) == 6)
				{
					it--;
					break;
				}
				else
				{
					SpeakerGroup.insert(make_pair(3, *it));
					break;
				}
			case 4:
				if (SpeakerGroup.count(4) == 6)
				{
					it--;
					break;
				}
				else
				{
					SpeakerGroup.insert(make_pair(4, *it));
					break;
				}
			default:
				break;
			}

		}
		vin.clear();
		cout << endl;
		cout << "----------------Group list-------------------" << endl;
		for (multimap<int, int>::iterator it = SpeakerGroup.begin(); it != SpeakerGroup.end(); it++)
		{
			cout << "The first" << (*it).first << "group" << " Member number:" << (*it).second << endl;
			vin.push_back((*it).second);
			
		}
		v = vin;
	}
	return 0;
}
bool cmp(const pair<int, Speaker>& p1, const pair<int, Speaker>& p2)
{
	if (p1.second.mScore == p2.second.mScore)
	{
		return p1.first < p2.first;
	}
	else 
	{
		return p1.second.mScore > p2.second.mScore;
	}
}
//Player competition
//index round, v1 list, mapSpeaker player information, v2 list for the next round
int speech_contest(int index, vector<int>& v1, map<int, Speaker>& mapSpeaker, vector<int>& v2)
{
	//cout << index << endl;
	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		deque<int> dscore;
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;
			dscore.push_back(score);
		}
		{
			int score = rand() % 41 + 60;
			dscore.push_back(score);
		}

		//The default sort is from small to large
		sort(dscore.begin(), dscore.end());

		dscore.pop_back();
		dscore.pop_front();

		int totalScore = 0;
		for (deque<int>::iterator it = dscore.begin(); it != dscore.end(); it++)
		{
			totalScore += (*it);
		}

		int avgScore = totalScore / dscore.size();

		map<int, Speaker>::iterator player = mapSpeaker.find(*it);
		(*player).second.mScore = avgScore;

	}

	int groupnub = 0;
	switch (index)
	{
		case 0:
			groupnub = 4;
			break;
		case 1:
			groupnub = 2;
			break;
		case 2:
			groupnub = 1;
			break;
	default:
		break;
	}

	map<int, Speaker> temp1;

	//cout << groupnub << endl;

	for (int j = 0; j < groupnub; j++)
	{
		vector<int> temp;
		temp.assign(v1.begin()+j*6, v1.begin() + (j+1)*6);

		cout << "-------------The first" << j + 1 << "Group competition results------------" << endl;
		for (vector<int>::iterator it = temp.begin(); it != temp.end(); it++)
		{
			//cout << *it << " ";
			map<int, Speaker>::iterator gmember = mapSpeaker.find(*it);
			temp1.insert(make_pair((*gmember).first, (*gmember).second));

		}
		cout << endl;

		//The map is a red black tree with a user-defined order inside. It cannot be sorted by sort. You can use the vector container for transitional sorting
		vector<pair<int, Speaker>> vpi(temp1.begin(), temp1.end());
		sort(vpi.begin(), vpi.end(), cmp);

		//v2.clear();
		int i = 0;
		for (vector<pair<int, Speaker>>::iterator it = vpi.begin(); it != vpi.end(); it++, i++)
		{
			cout << "ID:" << (*it).first << " Sex Name:" << (*it).second.mName << " Achievement:" << (*it).second.mScore << endl;

			if (i < 3)
			{
				v2.push_back((*it).first);
			}
		}
		i = 0;
		temp.clear();
		temp1.clear();
		vpi.clear();
	}
		return 0;
}
//Print the promotion list of players
int speech_contest_print(int index, vector<int> v, map<int, Speaker>& mapSpeaker)
{
	int j = (index+1) * 6;
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		map<int, Speaker>::iterator winer = mapSpeaker.find(*it);
		cout << "number:" << (*winer).first << " full name:" << (*winer).second.mName <<"Achievement:" << (*winer).second.mScore<<endl;

	}
	return 0;
}
int main()
{
	map<int, Speaker > mapSpeaker;
    vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	vector<int> v4;

	//Generate players
	GenSpeaker(mapSpeaker, v1);
	//Players in the first round draw lots to check the competition results (scores in the promotion list)
	cout << "\n\n\n any key,Start the first round" << endl;
	cin.get();
	cout << "------------First round-----------------" << endl;
	speech_contest_draw(v1);
	speech_contest(0, v1, mapSpeaker, v2);
	cout << "-----------Promotion list----------------" << endl;
	speech_contest_print(0, v2, mapSpeaker);
	//The players in the second round draw lots to check the results
	cout << "\n\n\n any key,Start the second round" << endl;
	cin.get();
	cout << "------------Second round-----------------" << endl;
	speech_contest_draw(v2);
	speech_contest(1, v2, mapSpeaker, v3);
	cout << "-------------Promotion list---------------" << endl;
	speech_contest_print(1, v3, mapSpeaker);
	//In the third round, the players draw lots to check the results
	cout << "\n\n\n any key,Start the third round" << endl;
	cin.get();
	cout << "---------------Third round---------------" << endl;
	speech_contest_draw(v3);
	speech_contest(2, v3, mapSpeaker, v4);
	cout << "--------------Top three in the competition-----------------" << endl;
	speech_contest_print(2, v4, mapSpeaker);
	return 0;
}

Topics: C++ Back-end