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;
}

输出结果(不唯一)

其他

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

相关推荐
3DVisionary1 天前
混凝土裂纹如何全自动识别?DIC技术在结构裂缝重构的应用
人工智能·学习·dic技术·混凝土裂缝监测·全场应变分析·三维位移测量·实验力学
鹏北海-RemHusband1 天前
Go 语言基础笔记 — 面向 JS/TS 前端开发者
笔记·golang
Trouvaille ~1 天前
【优选算法篇】深入浅出链表算法:交换、重排与合并的终极策略
c++·算法·链表·面试·蓝桥杯·笔试·后端开发
魔法阵维护师1 天前
从零开发游戏需要学习的c#模块,第二十八章(血条显示 —— 敌人与玩家生命可视化)
学习·游戏·c#
RuiZN1 天前
UE5 蓝图 FPS 01 Event Tick
c++·ue5
sheeta19981 天前
LeetCode 每日一题笔记 日期:2026.05.28 题目:3093. 最长公共后缀查询
linux·笔记·leetcode
A charmer1 天前
零基础学OC:变量与基本数据类型(C++开发者速通版)[特殊字符]
开发语言·c++·objective-c
墨白曦煜1 天前
算法实战笔记:链表的底层逻辑与指针的高阶玩法(二)
笔记·算法·链表
AOwhisky1 天前
Ceph系列第一期:Ceph分布式存储核心概念与架构初识
linux·运维·笔记·分布式·ceph·学习·架构
Bechamz1 天前
大数据开发学习Day44
大数据·学习