题目描述
给出n个人,每个人有个学号和成绩,现在要求按成绩从大到小排序后,成绩一样的按学号从小到大排
样例输入
4
1 125
3 90
4 138
2 125
样例输出
4 138 1
1 125 2
2 125 2
3 90 4
思路
就结构体排序
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n;
struct qq
{
int num, x;
}a[1005];
bool cmp(qq a, qq b)
{
if(a.x != b.x) return a.x > b.x;
return a.num < b.num;
}
int main()
{
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &a[i].num, &a[i].x);
sort(a + 1, a + n + 1, cmp);
int now = 1, w = 0;
for(int i = 1; i <= n; ++i)
{
printf("%d %d %d\n", a[i].num, a[i].x, now);
if(a[i + 1].x != a[i].x) now = i + 1;//不是相同成绩的要改排名
}
return 0;
}