解题思路:
使用类的思想,其他的就不说了别的地方都有写。
注意事项:
参考代码:
#include<iostream>
using namespace std;
class Student {
private:
string name;
int age;
float score;
static int N;
public:
Student(string n = "name", int a = 0, float s = 0)
{
name = n;
age = a;
score = s;
}
void getdata(string n, int a, float s)
{
name = n;
age = a;
score = s;
}
void show()
{
cout << "name:" << name;
cout << " age:" << age;
cout << " score:" << score << endl;
}
static void show(Student* stus)
{
Student* p = stus;
for (int i = 0; i < N; i++,p++)
{
cout << p->name << " " << p->age <<
" " << p->score << endl;
}
}
static void swap(Student* q, Student* p)
{
Student tem;
tem = *q; //cout << 1 << endl;
*q = *p; //cout << 2 << endl;
*p = tem; //cout << 3 << endl;
}
static void setN(int n)
{
N = n;
}
static void sort(Student* p)
{
for (int i = 0; i < N-1; i++)
{
for (int j = 0; j < N - 1 - i; j++)
{
if ((p + j)->score > (p + j + 1)->score ||
(p + j)->score == (p + j + 1)->score &&
(p+j)->name > (p+j+1)->name ||
(p + j)->score == (p + j + 1)->score &&
(p + j)->name == (p + j + 1)->name && (p+j)->age > (p+j+1)->age)
{
swap((p + j), (p + j + 1));
}
}
}
}
};
int Student::N = 0;
int test(int n)
{
Student::setN(n);
Student* stus, * p;
stus = new Student[n];
p = stus;
for (int i = 0; i < n; p++, i++)
{
string n;
int a;
float s;
cin >> n >> a >> s;
p->getdata(n, a, s);
}
p = stus;
Student::sort(p);
Student::show(stus);
delete []stus;
return 0;
}
int main()
{
int n;
while (cin >> n)
{
test(n);
}
return 0;
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复