c++ STL容器应用——评委打分案例

1. 案例描述

      评委给5个人打分,去掉一个最高分和一个最低分后计算平均分,平均分为选手的最终得分。

2. 代码实现

#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<algorithm>

using namespace std;

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

    string m_name;
    int m_score;
};


void set_score(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);
        }

        sort(d.begin(), d.end());
        d.pop_back();
        d.pop_front();

        int sum = 0;
        for(int i=0; i<d.size(); i++)
        {
            sum += d[i];
        }

        int avg = sum / 8;
        (*it).m_score = avg;
    }
}


void info_show(vector<Person>&v)
{
    for(int i=0; i < v.size(); i++)
    {
        cout << v[i].m_name << ":" << v[i].m_score << endl;
    }
}


int main()
{
    string name_seed = "ABCDE";
    vector<Person>v;
    for(int i=0; i < 5; i++)
    {
        string name = "选手-";
        name += name_seed[i];
        int score = 0;
        Person p(name, score);
        v.push_back(p);
    }


    set_score(v);
    info_show(v);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值