C++复习笔记--STL练习案例1(评委打分和员工分组)

2023-11-16

目录

1--评委打分

1-1--案例描述

1-2--代码实现

2--员工分组

2-1--案例分析

2-2--代码实现


1--评委打分

1-1--案例描述

        有 A, B, C, D, E 共 5 名选手,由 10 个评委分别对每一名选手打分,去除评委的最高分和最低分,取剩下 8 名评委的平均分作为最终选手的评分;

1-2--代码实现

#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>

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

void createPerson(std::vector<Person>& v){
    std::string nameSeed = "ABCDE";
    for(int i = 0; i < 5; i++){
        std::string name = "player";
        name += nameSeed[i];
        int score = 0;
        Person p(name, score);
        v.push_back(p);
    }
}

void setScore(std::vector<Person>&v){
    for(std::vector<Person>::iterator it = v.begin(); it != v.end(); it++){
        // 将评委的分数放入到deque容器中
        std::deque<int>d;
        for(int i = 0; i < 10; i++){
            int score = rand()%41 + 60; // 60 -100
            d.push_back(score);
        }
        std::cout<< "player: " << (*it).name << ", score: " << std::endl;
        for(std::deque<int>::iterator it = d.begin(); it != d.end(); it++){
            std::cout << *it << " ";
        }
        std::cout << std::endl;
        
        // 排序
        std::sort(d.begin(), d.end());
        // 去除最高分和最低分
        d.pop_back();
        d.pop_front();
        // 取平均分
        int sum = 0;
        for(std::deque<int>::iterator dit = d.begin(); dit != d.end(); dit++){
            sum += *dit;
        }
        int avg = sum / d.size();
        it->score = avg;
    }
}

void showScore(std::vector<Person> &v){
    std::cout << "Final Score: " << std::endl;
    for(std::vector<Person>::iterator it = v.begin(); it != v.end(); it++){
        std::cout << "name: " << (*it).name << ", socre: " << (*it).score << std::endl;
    }
}
int main(int argc, char* argv[]){

    srand( (unsigned int)time(NULL));
    // 创建 5 名选手
    std::vector<Person>v;
    createPerson(v);

    // 给选手打分
    setScore(v);

    // 显示最终得分
    showScore(v);
    return 0;
}

2--员工分组

2-1--案例分析

        公司招聘 10 个员工(ABCDEFGHIJ),员工信息包括姓名和工资,需要将员工分配到策划、美术和研发三个部门;通过 multimap 容器进行员工信息的插入,并分部分显示员工的信息;

2-2--代码实现

#include "iostream"
#include "string"
#include "vector"
#include "map"
#include "ctime"

class Person{
public:
    std::string Name;
    int Salary;
};

void CreateWorker(std::vector<Person> &v){
    std::string nameseed = "ABCDEFGHIJ";
    for(int i = 0; i < 10; i++){
        Person p;
        p.Name = "Worker";
        p.Name += nameseed[i];
        p.Salary = rand() % 10000 + 10000; // 10000 ~ 19999
        v.push_back(p);
    }
}

void SetGroup(std::vector<Person> &v, std::multimap<int, Person> &m){
    for(std::vector<Person>::iterator it = v.begin(); it != v.end(); it++){
        int ID = rand() % 3; // 0, 1, 2
        m.insert(std::make_pair(ID, *it));
    }
}

void ShowWorkerByGroup(std::multimap<int, Person> &m){
    std::cout << "Group of design: " << std::endl;
    std::multimap<int, Person>::iterator pos = m.find(0);
    int count = m.count(0);
    for(int i = 0; pos != m.end() && i < count; pos++, i++){
        std::cout << "Group: " << pos->first << " Name: " << pos->second.Name << " Salary: " << pos->second.Salary << std::endl;
    }

    std::cout << "Group of Art: " << std::endl;
    pos = m.find(1);
    count = m.count(1);
    for(int i = 0; pos != m.end() && i < count; pos++, i++){
        std::cout << "Group: " << pos->first << " Name: " << pos->second.Name << " Salary: " << pos->second.Salary << std::endl;
    }

    std::cout << "Group of research: " << std::endl;
    pos = m.find(2);
    count = m.count(2);
    for(int i = 0; pos != m.end() && i < count; pos++, i++){
        std::cout << "Group: " << pos->first << " Name: " << pos->second.Name << " Salary: " << pos->second.Salary << std::endl;
    }
}

int main(int argc, char* argv[]){
    srand((unsigned int) time(NULL));
    std::vector<Person> v;
    CreateWorker(v);

    std::multimap<int, Person> m;
    SetGroup(v, m);
    ShowWorkerByGroup(m);
    return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++复习笔记--STL练习案例1(评委打分和员工分组) 的相关文章

  • 在 HKCR 中创建新密钥有效,但不起作用

    我有以下代码 它返回 成功 但使用两种不同的工具使用搜索字符串 3BDAAC43 E734 11D5 93AF 00105A990292 搜索注册表不会产生任何结果 RegistryKey RK Registry ClassesRoot C
  • 使用 CMake 时如何导出 Emscripten 中的 C 函数

    In 本教程 https emscripten org docs porting connecting cpp and javascript Interacting with code html interacting with code
  • 在 CPP 类中将 C 函数声明为友元

    我需要在 C 函数中使用类的私有变量 我正在做这样的事情 class Helper private std string name public std getName return name friend extern C void in
  • 如何在类文件中使用 Url.Action() ?

    如何在 MVC 项目的类文件中使用 Url Action Like namespace 3harf public class myFunction public static void CheckUserAdminPanelPermissi
  • 按扩展名过滤搜索文件返回太多结果

    我正在开发一个 C 控制台应用程序 它必须管理 Windows 操作系统上的文件 我需要获取具有特定扩展名的文件名 列表 我找到了很多解决方案 最建议的是以下一种 HANDLE hFind WIN32 FIND DATA data hFin
  • 前向声明类型和“已声明为类类型的非类类型”

    我对以下代码有问题 template
  • 未找到 Boost 库,但编译正常

    我正在尝试在 C 中使用 boost 的文件系统 使用时看起来编译没问题 c c Analyse c o Analyse o g W Wall L usr local lib lboost filesystem lboost system
  • 传递 constexpr 对象

    我决定给予新的C 14的定义constexpr旋转并充分利用它 我决定编写一个小的编译时字符串解析器 然而 我正在努力保持我的对象constexpr将其传递给函数时 考虑以下代码 include
  • 无法注册时间触发的后台任务

    对于 Windows 8 应用程序 在 C Xaml 中 我尝试注册后台任务 很难说 但我想我的后台任务已正确注册 但是当我单击调试位置工具栏上的后台任务名称时 我的应用程序停止工作 没有任何消息 我查看了事件查看器上的日志 得到 具有入口
  • RestSharp获取序列化输出

    我正在寻找一种方法来访问 AddBody 调用的序列化结果 我正在使用内置的 RestSharp 序列化器 例子 class Foo public string FooField void SendRecord var f new Foo
  • 什么是空终止字符串?

    它与什么不同标准 字符串 http www cplusplus com reference string string 字符串 实际上只是一个数组chars 空终止字符串是指其中包含空字符的字符串 0 标记字符串的结尾 不一定是数组的结尾
  • 如何使用 ASP.NET Core 获取其他用户的声明

    我仍在学习 ASP NET Core 的身份 我正在进行基于声明的令牌授权 大多数示例都是关于 当前 登录用户的 就我而言 我的 RPC 服务正在接收身份数据库中某个用户的用户名和密码 我需要 验证是否存在具有此类凭据的用户 获取该用户的所
  • 如何使用 x64 运行 cl?

    我遇到了和这里同样的问题致命错误 C1034 windows h 未设置包含路径 https stackoverflow com questions 931652 fatal error c1034 windows h no include
  • 从 C# 使用 Odbc 调用 Oracle 包函数

    我在 Oracle 包中定义了一个函数 CREATE OR REPLACE PACKAGE BODY TESTUSER TESTPKG as FUNCTION testfunc n IN NUMBER RETURN NUMBER as be
  • 如何最好地以编程方式将 `__attribute__ ((unused))` 应用于这些自动生成的对象?

    In my makefile我有以下目标 它将文本 HTML 资源 编译 为unsigned char数组使用xxd i http linuxcommand org man pages xxd1 html 我将结果包装在匿名命名空间和标头保
  • Oauth2中如何同时撤销RefreshToken和使AccessToken失效

    我正在使用 Owin Oauth2 授权和资源服务器相同 开发单页面应用程序 AngularJS Net MVC Json Rest API 的身份验证流程 我选择了 Bearer Token 路由而不是传统的 cookie session
  • 模板类中的无效数据类型生成编译时错误?

    我正在使用 C 创建一个字符串类 我希望该类仅接受数据类型 char 和 wchar t 并且我希望编译器在编译时使用 error 捕获任何无效数据类型 我不喜欢使用assert 我怎样才能做到这一点 您可以使用静态断言 促进提供一个 ht
  • 代码中的.net Access Forms身份验证“超时”值

    我正在向我的应用程序添加注销过期警报 并希望从我的代码访问我的 web config 表单身份验证 超时 值 我有什么办法可以做到这一点吗 我认为您可以从 FormsAuthentication 静态类方法中读取它 这比直接读取 web c
  • 了解 Lambda 表达式和委托 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我已经尝试解决这个问题很长一段时间了 阅读在线博客和文章 但到目前为止还没有成功 什么是代表 什么是 Lambda 表达式 两者的优点
  • 在 System.Type 上使用条件断点时出错

    这是函数 public void Init System Type Type this Type Type BuildFieldAttributes BuildDataColumns FieldAttributes 我在第一行设置了一个断点

随机推荐