C++,STL 029(24.10.13)

内容

一道练习题。

(涉及string,vector,deque,sort)

题目(大致)

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

思路(总体)

(1)创建类Class来记录每个人的姓名和最终成绩(平均分);

(2)用vector容器来装入类Class;

(3)10个评委的打分用deque容器来记录并处理,从而得到最终成绩(平均分)。

代码

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctime>

using namespace std;

// 创建类Class来记录每个人的姓名和最终成绩(平均分)
class Person
{
public:
    string m_Name;
    int m_Score; // 最终成绩(平均分)

public:
    Person(string name, int score)
    {
        this->m_Name = name;
        this->m_Score = score;
    }
};

// 用vector容器来装入类Class,并对选手信息进行初步处理
void creatPerson(vector<Person> &v)
{
    string nameSeed = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        string name = "Player ";
        name += nameSeed[i];

        int score = 0;
        Person p(name, score);

        v.push_back(p);
    }
}

// 10个评委的打分用deque容器来记录并处理,从而得到最终成绩(平均分)
void setScore(vector<Person> &v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        deque<int> d;
        for (int i = 0; i < 10; i++)
        {
            int score = rand() % 41 + 60; // ???
            d.push_back(score);
        }

        /*
        检测成绩是否已成功录入
        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;
        }

        int avg = sum / d.size(); // 处理成绩总和来得到平均分

        it->m_Score = avg;
    }
}

// 输出结果
void showPerson(vector<Person> &v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名:" << it->m_Name << "  最终得分:" << it->m_Score << endl;
    }
}

int main()
{
    srand((unsigned int)time(NULL)); // ???

    vector<Person> v;

    creatPerson(v);
    setScore(v);
    showPerson(v);

    /*
    检测vector容器是否成功已装入 类Class
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << (*it).m_Name << " " << (*it).m_Score << endl;
    }
    */

    return 0;
}

输出结果(不唯一)

其他

关于代码中???的部分,待续。

相关推荐
John_ToDebug3 小时前
判断力无法被 AI 替代,但它正在被你亲手扔掉
经验分享·笔记
sunshine22 girl3 小时前
Java学习二 基本语法2, 算术运算符
java·学习
anew___4 小时前
蜂鸣器播放音乐——让 Arduino 唱出旋律
c++·stm32·单片机·嵌入式硬件·c
柳鲲鹏4 小时前
运气学概论:提纲草案
笔记
stolentime5 小时前
洛谷P10515 转圈题解
c++·算法·数学建模·贪心算法
tdtsmt6 小时前
穿戴硬件量产工艺实录:天地通电子智能手表主板 SMT 贴片案
c++
衡石科技6 小时前
Data_Agent记忆机制与经验复用衡石分析智能体会话记忆与长期学习技术解析
人工智能·科技·学习·算法·企业级bi
传奇开心果编程7 小时前
【Xilem基础语法学与练】第7课:Xilem与Masonry底层构建技术解析
学习·rust·前端框架
疯狂打码的少年7 小时前
【计算机网络】子网掩码与子网划分计算(含CIDR表示法)
服务器·笔记·计算机网络·php
想要入门的程序猿7 小时前
回调函数学习
java·网络·学习