10名评委为5名选手打分问题

摘要:本文主要介绍了10名评委对5名选手进行打分问题的解决办法。

1、题目示意

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

2、解决办法

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

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

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

2.4. deque容器遍历一遍,累加分数,累加分数/d.size()

2.5. person.score = 平均分

3、代码示例

 1 #include <iostream>
 2 #include<string>
 3 #include<vector>
 4 #include<deque>
 5 #include<algorithm>
 6 #include<time.h>
 7 
 8 using namespace std;
 9 
10 class Person {
11 public:
12     string m_name;  //人名
13     int m_score;    //分数
14     Person(string name,int score):m_name(name),m_score(score) {}
15 };
16 
17 void creatPerson(vector<Person>&v) {
18     string nameseed = "ABCDE";
19     for (int i=0;i<5;i++)
20     {
21         string name = "选手";
22         name += nameseed[i];
23         int score = 0;
24 
25         Person p(name,score);
26 
27         v.push_back(p);
28     }
29 }
30 
31 void setScore(vector<Person>&v) {
32     for (vector<Person>::iterator it=v.begin();it!=v.end();it++)
33     {
34         deque<int>d;
35         for (int i=0;i<10;i++)
36         {
37             int score = rand() % 41 + 60;
38             d.push_back(score);
39         }
40         sort(d.begin(),d.end());//排序
41         d.pop_back();  //去除最高分和最低分
42         d.pop_front();
43 
44         int sum=0;
45         for (deque<int>::iterator dit=d.begin();dit!=d.end();dit++)
46         {
47             sum += *dit;
48         }
49         int avg = sum / d.size();
50         it->m_score = avg;
51     }
52 }
53 
54 void showScore(vector<Person>&v) {
55     for (vector<Person>::iterator it=v.begin();it!=v.end();it++)
56     {
57         cout << "姓名" << it->m_name << "分数" << it->m_score << endl;
58     }
59 }
60 
61 int main() {
62     srand((unsigned)time(NULL));
63     vector<Person>v; //创建一个容器,用于存放选手名字和分数(也就是person对象)
64     creatPerson(v); //创建选手,对v进行赋值,分数初始值为0
65     setScore(v);  //随机模拟打分
66     showScore(v);
67 
68     system("pause");
69     return 0;
70 }
原文地址:https://www.cnblogs.com/lzy820260594/p/11379851.html