黑马c++ STL部分 笔记(4) 案例-评委打分

简介: 黑马c++ STL部分 笔记(4) 案例-评委打分

--案例描述:

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

--实现步骤:

创建五名选手,放到vector中

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

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

deque容器遍历一遍,累加总分

获取平均分

// 案例-评委打分
/*
案例描述
有五名选手,选手ABCDE,10个评委分别对每一个选手打分,去除最高分和最低分,取平均分。
实现步骤
创建五名选手,放到vector中
遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
sort算法对deque容器中分数排序,去除最高分和最低分
deque容器遍历一遍,累加总分
获取平均分
*/
 
#include <bits/stdc++.h>
using namespace std;
class person
{
public:
  person(string name, double score)
  {
    this->name = name;
    this->score = score;
  }
  string name;  // 姓名
  double score; // 平均分
};
void createperson(vector<person> &v)
{ // 要用&,否则传不到实参中
  string nameseed = "ABCDE";
  for (int i = 0; i < 5; i++)
  {
    string name = "选手";
    name = name + nameseed[i];
    int score = 0;
    person p(name, score);
    // 将创建的person对象放入容器中
    v.push_back(p);
  }
}
void setscore(vector<person> &v)
{
  for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
  {
    // 将评委的分数放入deque中
    deque<int> d;
    for (int i = 0; i < 10; i++)
    {
      int score = rand() % 41 + 60; //  0+60~40+60=60~100
      d.push_back(score);
    }
    // 显示评委打分
    cout << (*it).name << "的打分分别为"
         << " ";
    for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
    {
      cout << (*dit) << " ";
    }
    cout << endl;
    // 排序
    sort(d.begin(), d.end());
    // 去除最高分和最低分
    d.pop_back();
    d.pop_front();
    // 取平均分
    int sum = 0;
    for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
    {
      sum += (*dit);
    }
    double avg = (sum * 1.0) / d.size(); // sum/8
    (*it).score = avg;
  }
}
int main()
{
  // 随机数种子
  srand((unsigned int)time(NULL));
  // 创建五名选手
  vector<person> v; // 存放选手的容器
  createperson(v);
  // 测试
  //  for(vector<person>::iterator it=v.begin();it!=v.end();it++){
  //    cout<<(*it).name<<" "<<(*it).score<<endl;
  //  }
  //  给五名选手打分
  setscore(v);
 
  // 显示最后得分
  for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
  {
    cout << (*it).name << " " << (*it).score << endl;
  }
}
/*
总结:
*/


相关文章
|
6天前
|
设计模式 存储 C++
【C++/STL】:stack/queue的使用及底层剖析&&双端队列&&容器适配器
【C++/STL】:stack/queue的使用及底层剖析&&双端队列&&容器适配器
25 2
|
4天前
|
C++ 容器
C++之评委打分案例(vector与deque容器练习)
C++之评委打分案例(vector与deque容器练习)
8 1
|
5天前
|
编译器 C语言 C++
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
C++ STL中list迭代器的实现
|
4天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
9 0
|
4天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
12 0
|
4天前
|
算法 搜索推荐 C++
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
14 0
|
4天前
|
C++
C++之员工分组案例
C++之员工分组案例
6 0
|
4天前
|
C++
C++案例简单通讯录
C++案例简单通讯录
5 0
|
5天前
|
C++
【C++航海王:追寻罗杰的编程之路】STL—next_permutation函数
【C++航海王:追寻罗杰的编程之路】STL—next_permutation函数
4 0
|
2天前
|
C++
【C++】日期类Date(详解)②
- `-=`通过复用`+=`实现,`Date operator-(int day)`则通过创建副本并调用`-=`。 - 前置`++`和后置`++`同样使用重载,类似地,前置`--`和后置`--`也复用了`+=`和`-=1`。 - 比较运算符重载如`&gt;`, `==`, `&lt;`, `&lt;=`, `!=`,通常只需实现两个,其他可通过复合逻辑得出。 - `Date`减`Date`返回天数,通过迭代较小日期直到与较大日期相等,记录步数和符号。 ``` 这是236个字符的摘要,符合240字符以内的要求,涵盖了日期类中运算符重载的主要实现。