1 #include<iostream> 2 #include<string> 3 #include<deque> 4 #include<vector> 5 #include<algorithm> 6 7 using namespace std; 8 9 //选手类 10 class Person 11 { 12 public: 13 Person(string name, int score) 14 { 15 this->m_Name = name; 16 this->m_Score = score; 17 } 18 19 20 string m_Name; //姓名 21 int m_Score; //分数 22 }; 23 24 //赋值 5名 25 void createPerson(vector<Person>& v) 26 { 27 string nameSeed = "ABCDE"; 28 for (int i = 0; i < 5; i++) 29 { 30 string name = "选手"; 31 name += nameSeed[i]; //选手赋值操作 32 33 34 int score = 0; 35 Person p(name, score); 36 37 //将创建的Person的对象 放入容器中 38 v.push_back(p); 39 } 40 } 41 42 //打分 43 void steScore(vector<Person>& v) 44 { 45 for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) 46 { 47 //将评委的分数放入到deque容器中 48 deque<int>d; 49 for (int i = 0; i < 10; i++) 50 { 51 int score = rand() % 40 + 60; //60-100 52 d.push_back(score); 53 } 54 cout << "选手: " << it->m_Name << "\t打分:" << endl; 55 56 for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) 57 { 58 cout << *dit << " "; 59 } 60 cout << endl; 61 62 63 64 65 //paixu排序 66 sort(d.begin(), d.end()); 67 68 //去除最高分和最低分 69 d.pop_back(); 70 d.pop_front(); 71 72 //取平均分 73 int sum = 0; 74 for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) 75 { 76 sum += *dit; //累加每个评委的分数 77 } 78 int avg = sum / d.size(); 79 80 //将平均分赋值给选手上 81 it->m_Score = avg; 82 83 } 84 } 85 void showScore(vector<Person>& v) 86 { 87 for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) 88 { 89 cout << "姓名:" << it->m_Name << "\t平均分:" << it->m_Score << endl; 90 } 91 } 92 93 int main() 94 { 95 //随机数种子 96 srand((unsigned int)time(NULL)); 97 //1.创建5名选手 98 vector<Person>v; //存放选手的容器 99 createPerson(v); 100 101 //测试 102 103 //for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) 104 //{ 105 // cout << "姓名:" << (*it).m_Name << "\t分数:" << (*it).m_Score << endl; 106 107 //} 108 //2.给5名选手打分 109 steScore(v); 110 111 112 //3.显示最后的得分 113 showScore(v); 114 115 116 system("pause"); 117 return 0; 118 }