黑马C++(STL)案例-评委打分

该代码示例使用C++编程,通过vector和deque数据结构管理选手和评委的评分。程序首先创建5名选手,然后为每位选手生成10个随机评分,存储在deque中并排序。接着,去除每个选手评分中的最高分和最低分,计算平均分并显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

案例描述 有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。

黑马的实现步骤

1. 创建五名选手,放到vector中

2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中

3. sort算法对deque容器中分数排序,去除最高和最低分

4. deque容器遍历一遍,累加总分

5. 获取平均分

我大致是按照黑马的步骤实现的,但我的评委打分的容器是放在每个选手的成员参数内的,可以相互关联。

代码如下:

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

//评委打分

class Person
{
public:
	Person() {}

	Person(string name, int score)
	{
		m_name = name;
		m_score = score;
	}
	/*void getScore()
	{
		for (int i = 0; i < 10; i++)
		{
			int score;

			cin >> score;
			d.push_back(score);
		}
	}*/

	//分数容器
	deque<int> d;

	string m_name;

	int m_score;


};


void creatPerson(vector<Person>& v)
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i++)
	{
		//string name = "选手";
		//name +=	nameSeed[i];

		string name;
		name += nameSeed[i];

		int score = 0;
		Person p(name, score);

		//放入vector容器中
		v.push_back(p);
	}
}

void setScore(vector<Person>& v)
{
	//遍历容器
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		//第一层遍历的是Person
		// 
		//输入分数
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60;  // 60 ~ 100
			(*it).d.push_back(score);
		}
		//排序
		sort(it->d.begin(), it->d.end());

		//删除最高分和最低分
		(*it).d.pop_back();
		(*it).d.pop_front();

		//记总分
		int score = 0;
		for (deque<int>::iterator dit = (*it).d.begin(); dit != (*it).d.end(); dit++)
		{
			score += (*dit);
		}
		//导入person内
		it->m_score = score / (*it).d.size();
	}

}


void showScore(vector<Person> v)
{
	//cout << "选手编号:" << v.m_name << " " << "分数" << p.m_score << endl;

	for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		//是person
		cout << "选手编号:" << (*it).m_name << " " << "分数:" << (*it).m_score << endl;
		//展示所有分数
		for (deque<int>::const_iterator dit = (*it).d.begin(); dit != (*it).d.end(); dit++)
		{
			//是deque
			cout << (*dit) << " ";
		}
		cout << endl;
		cout << endl;
	}
}


int main()
{
	vector<Person> v;
	creatPerson(v);
	setScore(v);
	showScore(v);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值