zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    hive分区学习
    pyspark的学习
    往hive表中插入数据以及导出数据
    【Pytest学习3】setup和teardown简单用法,fixture里面的scope等于setup,用yield等于teardown
    【Pytest学习2】 pytest用例设计规则,terminal中使用常见命令行参数,pycharm中使用常见的命令行参数
    Jmeter响应内容显示乱码问题的解决办法
    Jmeter(三)测试计划和线程组
    Jmeter(二)Jmeter目录介绍 & 元件介绍
    JMeter之Ramp-up Period(in seconds)说明
    badboy云盘下载链接
  • 原文地址:https://www.cnblogs.com/lzy820260594/p/11379851.html
Copyright © 2011-2022 走看看