美文网首页
案例-评委打分

案例-评委打分

作者: lxr_ | 来源:发表于2021-04-13 10:40 被阅读0次
#include<iostream>
using namespace std;

#include<time.h>

#include<vector>
#include<deque>
#include<algorithm>

//有5名选手ABCDE,10个评委分别对每一名选手打分,去除评委中最低分,取平均分

//1.创建五名选手,放到vector中
//2.遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
//3.sort算法对deque容器中分数排序,去除最高和最低分
//4.deque容器遍历一遍,累加总分
//5.获取平均分
class Person
{
public:
    Person(string name,int score)
    {
        this->m_Name = name;
        this->m_Score = score;
    }

    string m_Name;
    int m_Score;
};
//输出分数
void PrintPerson(vector<Person> v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名:" << it->m_Name << "\t" << "分数:" << it->m_Score << endl;
    }
}
//创建选手
void CreatPerson(vector<Person>& v)
{
    string nameSeed = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        
        string name = "选手";
        name += nameSeed[i];

        int score = 0;

        Person p(name, score);//创建Person
         
        v.push_back(p);//加入vector容器中
    }
}
//打分
void SetsScore(vector<Person>& v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        //cout << "请给" << it->m_Name << "打分:" << endl;
        //将评委的分数放到deque容器中
        cout << it->m_Name << "打分:" << endl;
        deque<int> d;
        for (int i = 0; i < 10; i++)
        {
            int score;

            /*cout << "裁判" << i + 1 << ":" << endl;
            cin >> score;*///用户输入分数

            score = rand() % 41 + 60;
            cout << score << "  ";

            d.push_back(score);
        }
        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 avgScore = sum / d.size();//平均分

        it->m_Score = avgScore;//平均分为最终分数

    }
}
int main()
{
    srand((unsigned int)time(NULL));
    //1.创建五名选手
    vector<Person> v;
    CreatPerson(v);

    //测试
    //PrintPerson(v);

    //2.给5名选手打分
    SetsScore(v);

    //3.显示最后得分
    PrintPerson(v);

    system("pause");
    return 0;
}

相关文章

网友评论

      本文标题:案例-评委打分

      本文链接:https://www.haomeiwen.com/subject/hyyqlltx.html