STL项目-学校演讲比赛


// 学校演讲比赛.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include 
#include "string"
#include "vector"
#include "algorithm"
#include "functional"
#include "list"
#include "iterator"
#include "map"
#include "deque"
#include "numeric"
using namespace std;

class speaker
{
public:
    string m_name;
    int m_score[3];

};

int GenSpeaker(map<int, speaker> &mapSpeaker, vector<int> &v)
{
    string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    random_shuffle(str.begin(), str.end());

    for (int i = 0; i < 24; i++)
    {
        speaker tmp;
        tmp.m_name = "选手";
        tmp.m_name += str[i];
        mapSpeaker.insert(pair<int, speaker>(100 + i, tmp));
    }

    for (int i = 0; i < 24; i++)
    {
        v.push_back(100 + i);
    }

    return 0;
}

//选手抽签
int speech_contest_draw(vector<int> &v)
{
    random_shuffle(v.begin(), v.end());
    return 0;
}

//选手比赛
int speech_contest(int index, map<int, speaker> &mapSpeaker,vector<int> &v1, vector<int> &v2)
{
    //小组的比赛得分 记录下来;求出前三名 和 后三名
    multimap<int, int, greater<int>> multmapGroup;
    int tmpCount = 0;
    for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
    {
        tmpCount++;
        //打分
        {
            deque<int> dscore;
            for (int j = 0; j < 10; j++)
            {
                int score = 60 + rand() % 40;
                dscore.push_back(score);
            }
            sort(dscore.begin(), dscore.end());
            dscore.pop_back();
            dscore.pop_front();
            //求平均分
            int scoresumm = accumulate(dscore.begin(), dscore.end(), 0);
            int scoreavg = scoresumm / dscore.size();
            mapSpeaker[*it].m_score[index] = scoreavg;//选手得分存入容器中
            multmapGroup.insert(pair<int, int>(scoreavg, *it));
        }

        //处理分组
        if (tmpCount % 6 == 0)
        {
            cout << "小组的比赛成绩:" << endl;
            for (multimap<int, int, greater<int>>::iterator mit = multmapGroup.begin(); mit != multmapGroup.end(); mit++)
            {
                //编号    姓名    得分
                cout << mit->second << "\t" << mapSpeaker[mit->second].m_name << "\t" << mit->first <<endl;
            }
            //前三名晋级
            while (multmapGroup.size() > 3)
            {
                multimap<int, int, greater<int>>::iterator it1 = multmapGroup.begin();
                v2.push_back(it1->second);//把前三名放到晋级名单中
                multmapGroup.erase(it1);
            }
            multmapGroup.clear();//清空本小组比赛成绩
        }

    }
    return 0;
}


//查看比赛结果
int speech_contest_print(int index, map<int, speaker> &mapSpeaker, vector<int> &v)
{
    printf("第%d轮,晋级名单\n", index +1);
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "参赛编号:" << *it << "\t" << mapSpeaker[*it].m_name << "\t"<< mapSpeaker[*it].m_score[index] <<endl;
    }
    return 0;
}
int main()
{
    //1.容器的设计
    map<int, speaker>    mapSpeaker;
    vector<int>            v1;//第1轮比赛 名单
    vector<int>            v2;//第2轮比赛 名单
    vector<int>            v3;//第3轮比赛 名单
    vector<int>            v4;//最后前三名 名单

    //产生选手 得到第1轮选手比赛名单
    GenSpeaker(mapSpeaker, v1);

    //第1轮:选手抽签、选手比赛、查看比赛结果
    cout << "\n\n\n任意键,开始第1轮比赛..." << endl;
    cin.get();
    speech_contest_draw(v1);
    speech_contest(0, mapSpeaker, v1, v2);
    speech_contest_print(0, mapSpeaker, v2);

    //第2轮:选手抽签、选手比赛、查看比赛结果
    cout << "\n\n\n任意键,开始第2轮比赛..." << endl;
    cin.get();
    speech_contest_draw(v2);
    speech_contest(1, mapSpeaker, v2, v3);
    speech_contest_print(1, mapSpeaker, v3);

    //第3轮:选手抽签、选手比赛、查看比赛结果
    cout << "\n\n\n任意键,开始决赛..." << endl;
    cin.get();
    speech_contest_draw(v3);
    speech_contest(2, mapSpeaker, v3, v4);
    speech_contest_print(2, mapSpeaker, v4);


    std::cout << "Hello World!\n"; 
}
STL