C++之评委打分案例(vector与deque容器练习)

简介: C++之评委打分案例(vector与deque容器练习)

一、实现目标

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

二、代码实现

#include <iostream>
 
using namespace std;
 
#include <vector>
#include <deque>
#include <algorithm>
#include <string>
#include <ctime>
 
// 有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分
class Person {
public:
    Person(string name, int score) {
        this->m_Name = name;
        this->m_Score = score;
    }
 
    string m_Name; //姓名
    int m_Score; //平均分
};
 
vector<Person> createPerson();
 
void printDeque(deque<int> &d);
 
void printVector(const vector<Person> &v) {
    for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++) {
        cout << "名字:" << (*it).m_Name << " 分数:" << (*it).m_Score << " ";
    }
    cout << endl;
}
 
// 创建五个选手
void createPerson(vector<Person> &v) {
    string nameSeed = "ABCDE";
    for (int i = 0; i < 5; ++i) {
        string name = "选手";
        name += nameSeed[i];
        int score = 0;
        Person p(name, score);
        v.push_back(p);
    }
 
}
 
// 打分
void setScore(vector<Person> &v) {
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
        cout << endl;
        // 将评委的分数,放入到deque容器中
        deque<int> d;
        for (int i = 0; i < 10; ++i) {
            int score = rand() % 41 + 60;
            d.push_back(score);
        }
        cout << "选手:" << it->m_Name << "打分: " << endl;
        printDeque(d);
        sort(d.begin(), d.end());
        cout << "选手:" << it->m_Name << "排序得分: " << endl;
        printDeque(d);
        //去除最高分和最低分
        d.pop_front();
        d.pop_back();
        cout << "选手:" << it->m_Name << "最后得分: " << endl;
        printDeque(d);
        int sum = 0;
        for (int i = 0; i < d.size(); ++i) {
            sum += d[i];
        }
        int avg = sum / d.size();
        (*it).m_Score = avg;
        cout << "选手:" << it->m_Name << "平均分: " << avg << endl;
    }
}
 
void printDeque(deque<int> &d) {
    for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++) {
        cout << *dit << " ";
    }
    cout << endl;
}
 
void test01() {
    // 随机数种子
    srand((unsigned int) time(NULL));
    // 1、创建五个选手
    vector<Person> v;
    createPerson(v);
    // 打印选手
    printVector(v);
    //2、打分
    setScore(v);
    //3、打印分数
    printVector(v);
}
 
 
int main() {
    test01();
    system("pause");
    return 0;
}
 
名字:选手A 分数:0 名字:选手B 分数:0 名字:选手C 分数:0 名字:选手D 分数:0 名字:选手E 分数:0
 
选手:选手A打分:
62 77 86 63 76 62 85 99 65 61
选手:选手A排序得分:
61 62 62 63 65 76 77 85 86 99
选手:选手A最后得分:
62 62 63 65 76 77 85 86
选手:选手A平均分: 72
 
选手:选手B打分:
83 96 81 86 89 88 95 84 95 72
选手:选手B排序得分:
72 81 83 84 86 88 89 95 95 96
选手:选手B最后得分: 
81 83 84 86 88 89 95 95 
选手:选手B平均分: 87
 
选手:选手C打分:
60 76 91 95 78 92 78 95 83 81
选手:选手C排序得分:
60 76 78 78 81 83 91 92 95 95
选手:选手C最后得分:
76 78 78 81 83 91 92 95
选手:选手C平均分: 84
 
选手:选手D打分:
78 77 73 83 65 78 88 92 70 60 
选手:选手D排序得分:
60 65 70 73 77 78 78 83 88 92
选手:选手D最后得分:
65 70 73 77 78 78 83 88
选手:选手D平均分: 76
 
选手:选手E打分:
94 72 83 87 90 68 99 73 61 75
选手:选手E排序得分:
61 68 72 73 75 83 87 90 94 99 
选手:选手E最后得分:
68 72 73 75 83 87 90 94
选手:选手E平均分: 80
名字:选手A 分数:72 名字:选手B 分数:87 名字:选手C 分数:84 名字:选手D 分数:76 名字:选手E 分数:80

相关文章
|
3天前
|
编译器 C++
【C++核心】指针和引用案例详解
这篇文章详细讲解了C++中指针和引用的概念、使用场景和操作技巧,包括指针的定义、指针与数组、指针与函数的关系,以及引用的基本使用、注意事项和作为函数参数和返回值的用法。
11 3
|
3天前
|
C++
【C++案例】一个项目掌握C++基础-通讯录管理系统
这篇文章通过一个通讯录管理系统的C++项目案例,详细介绍了如何使用C++实现添加、显示、删除、查找、修改和清空联系人等功能。
12 3
|
15天前
|
JavaScript 前端开发 测试技术
一个google Test文件C++语言案例
这篇文章我们来介绍一下真正的C++语言如何用GTest来实现单元测试。
11 0
|
1月前
|
C++ 容器
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——AVL树
【C++航海王:追寻罗杰的编程之路】关联式容器的底层结构——AVL树
25 5
|
1月前
|
存储 C++ 索引
|
2月前
|
存储 C++ 容器
开发与运维数组问题之C++标准库中提供数据容器作为数组的替代如何解决
开发与运维数组问题之C++标准库中提供数据容器作为数组的替代如何解决
44 5
|
1月前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
1月前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
34 0
|
1月前
|
存储 缓存 NoSQL
【C++】哈希容器
【C++】哈希容器
|
1月前
|
存储 设计模式 算法
【C++】deque以及优先级队列
【C++】deque以及优先级队列