题目: 在歌唱比赛中有10位选手,5个评委,选手的得分计算规则是去掉最高分和最低分后求平均分,计算输出每个选手在本次比赛中的平均分并且排序。
在歌唱比赛中有10位选手,5个评委,选手的得分计算规则是去掉最高分和最低分后求平均分,计算输出每个选手在本次比赛中的平均分并且排序。
刚开始参考 (https://blog.youkuaiyun.com/yinkaishikd/article/details/46620477?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161747151616780265452746%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=161747151616780265452746&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-2-46620477.pc_search_result_no_baidu_js&utm_term=C%2b%2b%E6%80%8E%E4%B9%88%E5%B0%86%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%95%B0%E6%8D%AE%E8%BE%93%E5%85%A5%E5%88%B0%E6%95%B0%E7%BB%84%E4%B8%AD%E8%AE%A1%E7%AE%97) 的介绍 通过循环实现
初步代码如下
#include <iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
class People {
public:
string name;
float Point[5] = {};
float Avg;
int ID;
int getID(){
return ID;
}
float GetAvg();
People();
People(int ID, string name);
People(const People& obj);
~People(void);
void Show()
{
Avg = GetAvg();
cout << "选手" << name << "信息如下:" << endl;
cout << "\t编号:" << ID << endl;
cout << "\t平均分:" << Avg << endl;
}
void in()
{
cin >> Point[0] >> Point[1] >> Point[2] >> Point[3] >> Point[4];
}
};
float People::GetAvg() {
sort(Point, Point + 5);
Avg = (Point[1] + Point[2] + Point[3]) / 3;
return Avg;
}
People::People(void){
}
People::People(int ID, string name){
ID = ID;
name = name;
}
People::People(const People& obj)
{
ID = obj.ID;
name = obj.name;
}
People::~People()
{
}
int main(){
int n = 0;
float AVG=0;
int ID;
string name;
cout << "请输入学生个数" << endl;
cin >> n;
People *ctor = new People[n];
// People *ctor[20];
for (int i = 0; i < n; i++){
cout << endl << "请输第" << i + 1 << "个选手的信息:" << endl;
cout << "编号:";
cin >> ID;
cout << flush << "姓名:";
cin >> name;
ctor[i].name = name;
ctor[i].ID = ID;
cout << flush << "请输入五个评委给的分数:";
ctor[i].in();
}
cout << endl << "以下是所有选手的排名信息:\n" << endl;
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n - j - 1; k++)
{
float Avg1 = ctor[k].GetAvg();
float Avg2 = ctor[k + 1].GetAvg();
if (Avg1 < Avg2)
{
People temp = ctor[k];
ctor[k] = ctor[k + 1];
ctor[k + 1] = temp;
}
}
}
for (int i = 0; i < n; i++){
ctor[i].Show();
}
delete[] ctor;
return 0;
}
这种方法运行以后发现无论是升序降序 最后一个被排序的对象的Avg值一直是0,一直找不到原因。然后参考了
https://blog.youkuaiyun.com/zsy162534/article/details/52100878
用Sort解决排序问题。实现输入输出排序,完成作业。
#include <iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
class People {
public:
string name;
float Point[5];
float Avg;
int ID;
float GetAvg();
People();
People(int ID, string name);
People(const People& obj);
~People(void);
void Show()
{
Avg = GetAvg();
cout << "选手" << name << "信息如下:" << endl;
cout << "\t编号:" << ID << endl;
cout << "\t平均分:" << Avg << endl;
}
void in()
{
cin >> Point[0] >> Point[1] >> Point[2] >> Point[3] >> Point[4];
}
};
float People::GetAvg() {
sort(Point, Point + 5);
Avg = (Point[1] + Point[2] + Point[3]) / 3;
return Avg;
}
People::People(void){
}
People::People(int ID, string name){
ID = ID;
name = name;
}
People::People(const People& obj)
{
ID = obj.ID;
name = obj.name;
}
People::~People()
{
}
int cmp(const void *a, const void *b)
{
return ((People *)b)->GetAvg() - ((People *)a)->GetAvg();
}
int main(){
int n = 0;
int ID;
string name;
cout << "请输入选手个数" << endl;
cin >> n;
People *ctor = new People[n];
// People *ctor[20];
for (int i = 0; i < n; i++){
cout << endl << "请输第" << i + 1 << "个选手的信息:" << endl;
cout << "编号:";
cin >> ID;
cout << flush << "姓名:";
cin >> name;
ctor[i].name = name;
ctor[i].ID = ID;
cout << flush << "请输入五个评委给的分数:";
ctor[i].in();
}
cout << endl << "以下是所有选手的排名信息:\n" << endl;
qsort(ctor, n, sizeof(ctor[0]), cmp);
for (int i = 0; i < n; i++){
ctor[i].Show();
}
delete[] ctor;
return 0;
}
记录学习过程,以后遇到类似问题能解决