案例描述:选手ABCDE,10个评委对每个选手打分,去除最高分,去除最低分并取平均值
#include#include #include #include 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 printperson(const vector &d1) { for (vector ::const_iterator it = d1.begin(); it != d1.end(); it++) { cout << (*it).m_name << "分数:" << (*it).m_score << endl; } cout << endl; } //创建选手类,包含其名字和最终得分 void createperson(vector & v) { string nameseed = "ABCDE"; for (int i = 0; i < 5; i++) { string name = "选手 "; name += nameseed[i]; int score = 0; person p(name, score); //将创建的人放入容器中 v.push_back(p); } } //创建打分类 int setscore(deque & v) { for (int i = 0; i < 10; i++) { v.push_back(rand() % 41 + 60);//60-100随机数 } //去除最高分和最低分 sort(v.begin(), v.end()); v.pop_front(); v.pop_back(); int final=0; for (deque ::iterator it = v.begin(); it != v.end(); it++) { final += *it; } final = final / v.size(); return final; } //deque容器的排序 void test1() { //创建五名选手 vector v; createperson(v); //评委打分,并去除最高分和最低分求其平均值 for (int i = 0; i < 5; i++) { deque d; int set=setscore(d); v[i].m_score = set; } printperson(v); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)