C++ 提高教程 STL-评委打分
Posted 行码阁119
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 提高教程 STL-评委打分相关的知识,希望对你有一定的参考价值。
# include<iostream>
# include<string>
# include<vector>
# include<deque>
# include<algorithm>
# include<ctime>
using namespace std;
class Person
{
public:
Person(string name, int ascore)
{
this->m_Name = name;
this->m_Ascore = ascore;
}
string m_Name;
int m_Ascore;
};
void creatPerson(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);
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;
d.push_back(score);
}
//cout << "选手:" << it->m_Name << "打分:" << endl;
//for (deque<int>::iterator itd = d.begin(); itd != d.end(); itd++)
//{
// cout << *itd << " ";
//}
//cout << endl;
sort(d.begin(), d.end());
//去除最高和最低分
d.pop_front();
d.pop_back();
//取平均分
int sum = 0;
for (deque<int>::iterator itd = d.begin(); itd != d.end(); itd++)
{
sum = sum + *itd;
}
it->m_Ascore = sum / d.size();
}
}
void showScore(vector<Person> &v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "选手姓名:"<<it->m_Name<<" 得分为:"<<it->m_Ascore << endl;
}
}
int main()
{
//1、创建5名选手
//随机数种子
srand((unsigned int)time(NULL));
vector<Person>v;
creatPerson(v);
//测试
//for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << "姓名:" << it->m_Name << " 平均分" << it->m_Ascore << endl;
//}
setScore(v);
//3、显示平均分
showScore(v);
system("pause");
return 0;
}
以上是关于C++ 提高教程 STL-评委打分的主要内容,如果未能解决你的问题,请参考以下文章
在编程竞赛中,有6个评委为参赛选手打分,分数为0-100的整数分。 选手的最后得分为:去掉一个最高分和一个最低分的4个评委平均值 * 请写代码实现(不考虑小数部分)