c++ 容器案例 评委打分

有五名选手ABCDE,分别有十位评委给其打分,最后去除最高分和最低分,给出平均分。

此案例用于C++容器的构造和方法调用复习,是个很不错的编程练习。

思路:

先创建一个person类:包含选手姓名与分数

class Person
{
    public:
    string name;
    int score;

    Person(string name,int score)
    {
        this->name=name;
        this->score=score;
    }

};

 创建vector容器,储存选手对象

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

 加载评委打分,采用deque容器,因为要去除最高分和最低分,因此deque容器是最佳选择

void setscore(vector<Person>&v)
{
    
    for (vector<Person>::iterator it=v.begin();it!=v.end();it++)
    {
        deque<int>deq;
        
        int score=rand()%41+60;
        for (int i=0;i<10;i++)
        {
            deq.push_back(score);
        }
        sort(deq.begin(),deq.end());
        deq.pop_front();
        deq.pop_back();
        int sum=0;
        for(int j=0;j<10;j++)
        {
            sum+=deq[j];
        }
        int ave_score=sum/deq.size();
        it->score=ave_score;
    }
}

最后就该打印选手对象的信息了

void printscore(vector<Person>&v)
{
    for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
    {
        cout<<it->name<<"  得分:  "<<it->score<<endl;
    }
}

 总代码如下:

#include<iostream>
#include<deque>
#include<vector>
#include<string>
#include<algorithm>
#include<time.h>
using namespace std;
class Person
{
    public:
    string name;
    int score;

    Person(string name,int score)
    {
        this->name=name;
        this->score=score;
    }

};

void setperson(vector<Person>&v)
{
    string seq="ABCDE";
    for(int i=0;i<5;i++)
    {
        string name="选手";
        name+=seq[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++)
    {
        deque<int>deq;//使用deque容器,方便排序后对其去除最高分和最低分
        
        int score=rand()%41+60;//设置随机分数,区间[60,100]
        for (int i=0;i<10;i++)//十个评委
        {
            deq.push_back(score);
        }
        sort(deq.begin(),deq.end());//排序,要包含头文件algorithm
        deq.pop_front();
        deq.pop_back();
        int sum=0;
        for(int j=0;j<10;j++)
        {
            sum+=deq[j];
        }
        int ave_score=sum/deq.size();
        it->score=ave_score;
    }
}
void printscore(vector<Person>&v)
{
    for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
    {
        cout<<it->name<<"  得分:  "<<it->score<<endl;
    }
}

int main()
{
    srand(time(NULL));//设置随机种子,不然每次的结果都一样
    vector<Person>v;//创建Vector容器储存五个选手对象,数据类型为Person
    setperson(v);//初始化vector
    setscore(v);//加载评委打分并做后续处理
    printscore(v);//输出最终打分

    return 0;
}

结果:

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值