输入
3
1 90
2 87
3 92
输出
2 87
1 90
3 92
【提交地址】
自定义类型介绍
使用struct定义一个结构体,里面定义需要的属性变量
在定义struct的时候,变量名首字母一般大写,规范。
struct Student{
int num;
int grade;
};
代码
#include <cstdio>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
struct Student{
int num;
int grade;
};
bool comp(Student lhs,Student rhs){
if (lhs.grade<rhs.grade){
return true;
}else if(lhs.grade==rhs.grade&&lhs.num<rhs.num) {
return true;
}else{
return false;
}
}
int main() {
int n;
Student arr[100];
scanf("%d",&n);
for (int i = 0; i < n; ++i) {
scanf("%d%d",&arr[i].num,&arr[i].grade);
}
sort(arr,arr+n,comp);
for (int i = 0; i < n; ++i) {
printf("%d %d\n",arr[i].num,arr[i].grade);
}
return 0;
}