Files
python/TangDou/XiTi/1354.cpp

42 lines
1.3 KiB
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
struct Student {
string name;
int height;
string num;
};
//对比的方法
/*
用大于号就是从大到小排序,用小于号就是从小到大排序
*/
bool compare(const Student &x, const Student &y) {
if (x.height == y.height) return x.num < y.num;
return x.height > y.height;
}
int main() {
int n;
cin >> n;
//声明动态数组
Student *a = new Student[n];
for (int i = 0; i < n; ++i) {
cin >> a[i].name >> a[i].height >> a[i].num;
}
/*
此函数有3个参数:
参数1:第一个参数是数组的首地址,一般写上数组名就可以,因为数组名是一个指针常量。
参数2:第二个参数相对较好理解,即首地址加上数组的长度n(代表尾地址的下一地址)。
参数3:默认可以不填,如果不填sort会默认按数组升序排序。也就是1,2,3,4排序。也可以自定义一个排序函数,
改排序方式为降序什么的,也就是4,3,2,1这样。
*/
sort(a, a + n, compare);
//输出结果
cout << a[0].name << " " << a[0].height << " " << a[0].num << endl;
//删除动态数组
delete[]a;
return 0;
}