#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctime>
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);
v.push_back(p);
}
}
void setScore (vector<Person> &v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
{
deque<int> d;
for (int i = 0; i < 10; i ++)
{
d.push_front(rand() % 41 + 60);
}
sort(d.begin(), d.end());
d.pop_front();
d.pop_back();
int sumScore = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit ++)
{
sumScore += *dit;
}
int score = sumScore / d.size();
// cout << score << endl;
it->m_score = score;
}
}
void showScore (vector<Person> v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); *it ++)
{
cout << it->m_name << it->m_score << endl;
}
}
int main ()
{
srand((unsigned int)time(NULL));
//创建5名选手
vector <Person> v;
createPerson(v);
// for (vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
// {
// cout << (*it).m_name;
// }
//给5名选手打分
setScore(v);
// for (vector<Person>::iterator it = v.begin(); it != v.end(); it ++)
// {
// cout << it->m_score << endl;
// }
//显示最后得分
showScore(v);
return 0;
}
23、STL案例:评委打分
最新推荐文章于 2024-11-07 15:40:17 发布