C++程序例:评委打分 - 关于vector和deque

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

class Person//选手类
{
public:

	Person(string name, int score)
	{
		this->m_Name = name;//姓名
		this->m_Score = score;//平均分
	}
	string m_Name;
	int m_Score;

};

void createPerson(vector<Person>& v)//实参想和传入的形参一起修改就必须用引用
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i++)
	{
		string name = "选手";
		name += nameSeed[i];

		int score = 0;
		Person p(name, score);
		//讲创建的五个对象(初始平均分都是0)放入vector容器中
		v.push_back(p);
	}

}

void setScore(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		//准备一个deque容器存放评委打的分数
		deque<int>d;
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;//随机数为0到40,分数为60到100
			d.push_back(score);
		}

		//把上面存放的打分都打印出来
		cout << "打分显示:" << it->m_Name << " 获得的10个评分如下:" << endl;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			cout << *dit << " ";
		}
		cout << endl;

		//去除最高分最低分
		sort(d.begin(), d.end());//注意#include<algorithm>,注意不需要返回

		//之所以用deque容器就是因为他是双向的,对头部数据有一个直接的接口front
		//而vector容器是没有针对头删的操作的

		d.pop_front();
		d.pop_back();

		//取平均分
		int sum = 0;
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;
		}
		int avg = sum / d.size();

		it->m_Score = avg;//把平均分给选手

	}
}

void showScore(vector<Person>& v)
{
	cout << endl;
	cout << "去除最高分、最低分后,对5名选手的8个得分进行平均:" << endl;
	int max = v.begin()->m_Score;
	string numOne = v.begin()->m_Name;
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (*it).m_Name << "最终得分为:" << (*it).m_Score << endl;
		if ((*it).m_Score > max)
		{
			max = (*it).m_Score;
			numOne = (*it).m_Name;
		}
	}
	cout << endl;
	cout << "本次大赛的冠军是:" << numOne << " 得分为:" << max << endl;


}

int main()
{

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

	//1、创建5名选手
	vector<Person>v;
	createPerson(v);//把5名选手放入容器

	/*//测试
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "系统初始化:共计" << v.size() << "名选手" << endl;
		cout << (*it).m_Name << " 的平均分初始化为:" << (*it).m_Score << endl;
	}
	cout << endl;*/

	setScore(v);

	showScore(v);

	system("pause");
	return 0;
}

结果:

打分显示:选手A 获得的10个评分如下:
80 94 100 93 94 77 83 76 97 82
打分显示:选手B 获得的10个评分如下:
90 65 91 89 62 69 95 87 87 80
打分显示:选手C 获得的10个评分如下:
90 77 91 87 60 60 84 84 90 62
打分显示:选手D 获得的10个评分如下:
94 70 99 77 93 77 66 95 94 92
打分显示:选手E 获得的10个评分如下:
64 63 68 61 60 62 60 85 97 67

去除最高分、最低分后,对5名选手的8个得分进行平均:
选手A最终得分为:87
选手B最终得分为:82
选手C最终得分为:79
选手D最终得分为:86
选手E最终得分为:66

本次大赛的冠军是:选手A 得分为:87
请按任意键继续. . .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值