STL案例1

1 /**********  ab.cpp  ************/
2 #include<iostream>
3 #include<vector>
4 #include<map>
5 #include<algorithm>
6 #include<deque>
7 #include<numeric>
8 using namespace std;
  9
10 class Speaker
11 {
12 public:
13     string m_Name;//姓名
14     int m_Score[3];//得分数组
15 };
16
17 void createSpeaker(vector<int>&v,map<int,Speaker>&m)
18 {
19     string nameSeed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
20     for(int i = 0;i < nameSeed.size(); i++)
21     {
22         string name = "选手";
23         name += nameSeed[i];
24         Speaker sp;
25         sp.m_Name = name;
26         for(int j = 0;j< 3; j++)
27         {
28             sp.m_Score[j] = 0;
29         }
30         v.push_back(i + 100);//编号 100~123
31         m.insert(make_pair(i + 100,sp));
32     }
33 }
34
35 void speechDraw(vector<int> &v)
36 {
37     //抽签
38     random_shuffle(v.begin(),v.end());
39 }
40 //1 index表示第几轮,2 v1是比赛选手编号,
41 //3 m是选手编号和对应的选手,4 v2是晋级选手编号
42 void speechContest(int index,vector<int>&v1,map<int,Speaker>&m,vector<int>&v2)
43 {
44     multimap<int ,int, greater<int>> groupMap;//key 分数 value 编号
45     int num = 0;
46     for(vector<int>::iterator it = v1.begin();it!=v1.end();it++)
47     {
48         num++;
49         deque<int>d;
50         for(int i = 0;i < 10;i++)
51         {
52             int score = rand() % 41 + 60;//60 ~ 100
53             d.push_back(score);
54         }
55         //排序
56         sort(d.begin(),d.end());
57         //去除最高最低分
58         d.pop_back();
59         d.pop_front();
60         //累计分数
61         int sum = accumulate(d.begin(),d.end(),0);
62         int avg = sum / d.size();
63
64         m[*it].m_Score[index - 1] = avg;
65
66         groupMap.insert(make_pair(avg,*it));
67
68         if(num % 6 == 0)
69         {
70             cout<<"小组比赛成绩如下:" << endl;
71             for(multimap<int,int,greater<int>>::iterator mit = groupMap.begin();mit!=groupMap.end();mit++)//在遍历的时候能够按照一定的顺序,大小的顺序。
72             {
73                 cout<<"编号:"<<mit->second <<姓名:"<< m[mit->second].m_Name <<" 得分:"<<m[mit->second].m_Score[index-1]<<endl;
74             }
75             //取前三名
76             int count = 0;
77             for(multimap<int,int,greater<int>>::iterator mit = groupMap.begin();mit!=groupMap.end();mit++)
78             {
79                 v2.push_back(mit->second);
80                 if(count++ == 2) break;
81             }
82             groupMap.clear();//清空临时容器
83         }
84     }
85     //v1 = v2;//使用晋级之后的选手更新v1
86     //v2.clear();//清除v2,以存放新的晋级选手
87 }
88 //v 晋级选手的编号
89 //m 各个选手的编号以及信息
90 void showScore(int index,vector<int>&v,map<int,Speaker>&m)
91 {
92     cout<<""<<index<<"轮 比赛成绩如下:"<<endl;
93     for(map<int,Speaker>::iterator it = m.begin();it!=m.end();it++)
94     {
95         cout<<"选手编号:" << it->first <<姓名:"<<it->second.m_Name<<"  分数:"<<it->second.m_Score[index-1]<<endl;
96     }
97     cout<<"晋级选手编号:"<<endl;
98     for(vector<int>::iterator it = v.begin();it!=v.end();it++)
99     {
100         cout<<*it <<endl;
101     }
102 }
103
104 void round(int index, map<int,Speaker>&m,vector<int>&v1, vector<int> &v2){
105     cout<<"---------------------------------------------------"<<endl;
106     //抽签
107     speechDraw(v1);
108     //比赛
109     speechContest(index,v1,m,v2);
110     //显示比赛结果
111     showScore(index,v2,m);
112     v1 = v2;
113     v2.clear();
114     cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl<<endl;
115 }
116
117 int main(){
118     vector<int>v1; //选手编号
119     map<int, Speaker>m;//存放选后编号和对应的选手
120     //创建选手
121     createSpeaker(v1,m);
122     vector<int> v2;//进入下一轮比赛的人员编号
123     for(int index = 1;index <=3;index++){
124         round(index,m,v1,v2);
125     }
126 /*
127     //抽签
128 speechDraw(v1);
129     //比赛
130 speechContest(1,v1,m,v2);
131     //显示比赛结果
132     showScore(1,v2,m);//轮数,晋级编号,具体的人员信息
133
134 v1 = v2;
135 v2.clear();
136 speechDraw(v1);
137 speechContest(2,v1,m,v2);
138 showScore(2,v2,m);
139
140
141 //for(map<int,Speaker>::iterator it = m.begin();it != m.end(); it++)
142 //{
143     //  cout<< "编号:" << it->first << "   姓名:" << it->second.m_Name <<endl;
144 //}
145 */
146     return 0;
147 }

 
/**********  ab.cpp  ************/
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<deque>
#include<numeric>
using namespace std;

class Speaker
{
public:
    string m_Name;//姓名
    int m_Score[3];//得分数组
};

void createSpeaker(vector<int>&v,map<int,Speaker>&m)
{
	string nameSeed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	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());
}
//1 index表示第几轮,2 v1是比赛选手编号,
//3 m是选手编号和对应的选手,4 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();

		m[*it].m_Score[index - 1] = avg;

		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();mit++)
			{
				v2.push_back(mit->second);
				if(count++ == 2) break;
			}
			groupMap.clear();//清空临时容器
		}
	}
	//v1 = v2;//使用晋级之后的选手更新v1
	//v2.clear();//清除v2,以存放新的晋级选手
}
//v 晋级选手的编号
//m 各个选手的编号以及信息
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;
	}
}

void round(int index, map<int,Speaker>&m,vector<int>&v1, vector<int> &v2){
	cout<<"---------------------------------------------------"<<endl;
	//抽签
	speechDraw(v1);
	//比赛
	speechContest(index,v1,m,v2);
	//显示比赛结果
	showScore(index,v2,m);
	v1 = v2;
	v2.clear();
	cout<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl<<endl;
}

int main(){
    vector<int>v1; //选手编号
    map<int, Speaker>m;//存放选后编号和对应的选手
    //创建选手
    createSpeaker(v1,m);
	vector<int> v2;//进入下一轮比赛的人员编号
	for(int index = 1;index <=3;index++){
		round(index,m,v1,v2);
	}
/*
	//抽签
	speechDraw(v1);
	//比赛
	speechContest(1,v1,m,v2);
	//显示比赛结果
	showScore(1,v2,m);//轮数,晋级编号,具体的人员信息

	v1 = v2;
	v2.clear();
	speechDraw(v1);
	speechContest(2,v1,m,v2);
	showScore(2,v2,m);


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

编译运行

acat@acat-xx:debate$ g++ -g -o ab ab.cpp 
acat@acat-xx:debate$ ./ab
运行结果
---------------------------------------------------
小组比赛成绩如下:
编号:110  姓名:选手K 得分:85
编号:116  姓名:选手Q 得分:84
编号:104  姓名:选手E 得分:82
编号:111  姓名:选手L 得分:80
编号:124  姓名:选手Y 得分:80
编号:115  姓名:选手P 得分:79
小组比赛成绩如下:
编号:106  姓名:选手G 得分:87
编号:109  姓名:选手J 得分:83
编号:117  姓名:选手R 得分:82
编号:103  姓名:选手D 得分:80
编号:122  姓名:选手W 得分:77
编号:101  姓名:选手B 得分:72
小组比赛成绩如下:
编号:100  姓名:选手A 得分:82
编号:118  姓名:选手S 得分:82
编号:125  姓名:选手Z 得分:81
编号:119  姓名:选手T 得分:80
编号:123  姓名:选手X 得分:80
编号:102  姓名:选手C 得分:79
小组比赛成绩如下:
编号:112  姓名:选手M 得分:84
编号:107  姓名:选手H 得分:80
编号:108  姓名:选手I 得分:77
编号:105  姓名:选手F 得分:77
编号:121  姓名:选手V 得分:77
编号:113  姓名:选手N 得分:76
第1轮 比赛成绩如下:
选手编号:100  姓名:选手A  分数:82
选手编号:101  姓名:选手B  分数:72
选手编号:102  姓名:选手C  分数:79
选手编号:103  姓名:选手D  分数:80
选手编号:104  姓名:选手E  分数:82
选手编号:105  姓名:选手F  分数:77
选手编号:106  姓名:选手G  分数:87
选手编号:107  姓名:选手H  分数:80
选手编号:108  姓名:选手I  分数:77
选手编号:109  姓名:选手J  分数:83
选手编号:110  姓名:选手K  分数:85
选手编号:111  姓名:选手L  分数:80
选手编号:112  姓名:选手M  分数:84
选手编号:113  姓名:选手N  分数:76
选手编号:114  姓名:选手O  分数:81
选手编号:115  姓名:选手P  分数:79
选手编号:116  姓名:选手Q  分数:84
选手编号:117  姓名:选手R  分数:82
选手编号:118  姓名:选手S  分数:82
选手编号:119  姓名:选手T  分数:80
选手编号:120  姓名:选手U  分数:75
选手编号:121  姓名:选手V  分数:77
选手编号:122  姓名:选手W  分数:77
选手编号:123  姓名:选手X  分数:80
选手编号:124  姓名:选手Y  分数:80
选手编号:125  姓名:选手Z  分数:81
晋级选手编号:
110
116
104
106
109
117
100
118
125
112
107
108
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

---------------------------------------------------
小组比赛成绩如下:
编号:118  姓名:选手S 得分:83
编号:100  姓名:选手A 得分:81
编号:125  姓名:选手Z 得分:77
编号:116  姓名:选手Q 得分:75
编号:110  姓名:选手K 得分:72
编号:107  姓名:选手H 得分:72
小组比赛成绩如下:
编号:106  姓名:选手G 得分:83
编号:112  姓名:选手M 得分:80
编号:117  姓名:选手R 得分:80
编号:104  姓名:选手E 得分:78
编号:109  姓名:选手J 得分:77
编号:108  姓名:选手I 得分:72
第2轮 比赛成绩如下:
选手编号:100  姓名:选手A  分数:81
选手编号:101  姓名:选手B  分数:0
选手编号:102  姓名:选手C  分数:0
选手编号:103  姓名:选手D  分数:0
选手编号:104  姓名:选手E  分数:78
选手编号:105  姓名:选手F  分数:0
选手编号:106  姓名:选手G  分数:83
选手编号:107  姓名:选手H  分数:72
选手编号:108  姓名:选手I  分数:72
选手编号:109  姓名:选手J  分数:77
选手编号:110  姓名:选手K  分数:72
选手编号:111  姓名:选手L  分数:0
选手编号:112  姓名:选手M  分数:80
选手编号:113  姓名:选手N  分数:0
选手编号:114  姓名:选手O  分数:0
选手编号:115  姓名:选手P  分数:0
选手编号:116  姓名:选手Q  分数:75
选手编号:117  姓名:选手R  分数:80
选手编号:118  姓名:选手S  分数:83
选手编号:119  姓名:选手T  分数:0
选手编号:120  姓名:选手U  分数:0
选手编号:121  姓名:选手V  分数:0
选手编号:122  姓名:选手W  分数:0
选手编号:123  姓名:选手X  分数:0
选手编号:124  姓名:选手Y  分数:0
选手编号:125  姓名:选手Z  分数:77
晋级选手编号:
118
100
125
106
112
117
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

---------------------------------------------------
小组比赛成绩如下:
编号:125  姓名:选手Z 得分:87
编号:106  姓名:选手G 得分:86
编号:112  姓名:选手M 得分:85
编号:118  姓名:选手S 得分:84
编号:100  姓名:选手A 得分:77
编号:117  姓名:选手R 得分:77
第3轮 比赛成绩如下:
选手编号:100  姓名:选手A  分数:77
选手编号:101  姓名:选手B  分数:0
选手编号:102  姓名:选手C  分数:0
选手编号:103  姓名:选手D  分数:0
选手编号:104  姓名:选手E  分数:0
选手编号:105  姓名:选手F  分数:0
选手编号:106  姓名:选手G  分数:86
选手编号:107  姓名:选手H  分数:0
选手编号:108  姓名:选手I  分数:0
选手编号:109  姓名:选手J  分数:0
选手编号:110  姓名:选手K  分数:0
选手编号:111  姓名:选手L  分数:0
选手编号:112  姓名:选手M  分数:85
选手编号:113  姓名:选手N  分数:0
选手编号:114  姓名:选手O  分数:0
选手编号:115  姓名:选手P  分数:0
选手编号:116  姓名:选手Q  分数:0
选手编号:117  姓名:选手R  分数:77
选手编号:118  姓名:选手S  分数:84
选手编号:119  姓名:选手T  分数:0
选手编号:120  姓名:选手U  分数:0
选手编号:121  姓名:选手V  分数:0
选手编号:122  姓名:选手W  分数:0
选手编号:123  姓名:选手X  分数:0
选手编号:124  姓名:选手Y  分数:0
选手编号:125  姓名:选手Z  分数:87
晋级选手编号:
125
106
112
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值