提交 0aeb20a8 编写于 作者: 每日一练社区's avatar 每日一练社区

add exercises

上级 4a74c7a3
## 标题
结构体排序
## 题目描述
输入学生名字和分数,对其降序排序
#### 输入
```
zhangsan 100
lisi 60
wangwu 80
```
#### 输出
\ No newline at end of file
```
zhangsan 100
wangwu 80
lisi 60
```
\ No newline at end of file
#include <iostream>
#include <algorithm>
#define N 100
using namespace std;
struct student
{
string name;
int score;
};
bool sortrule(student a, student b)
{
if (a.score > b.score)
{
return true;
}
else if (a.score < b.score)
{
return false;
}
else if (a.score == b.score)
{
if (a.name < b.name)
return true;
else
return false;
}
}
int main()
{
student stu[N];
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> stu[i].name >> stu[i].score;
}
sort(stu, stu + n, sortrule);
for (int i = 0; i < n; i++)
{
cout << stu[i].name << " " << stu[i].score << endl;
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册