ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ //案列-评委打分 //(容器添加 删除 算法排序 随机数 字符串追加)描述:5名选手 ABCDE,10个评委分别对每一位选手打分,去除最高分,去除评委中的 //的最低分,取平均分

2021-08-15 11:00:27  阅读:239  来源: 互联网

标签:score ++ int 选手 vector 去除 dit 评委 打分


  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 }

 

标签:score,++,int,选手,vector,去除,dit,评委,打分
来源: https://www.cnblogs.com/Bytezero/p/15142863.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有