Problem Description
第四届山东理工大学ACM网络编程擂台赛比赛完后需要产生一个最终排名,排名按照题数多少来决定。但是有太多的队伍参与,手动计算排名已经不能满足比赛的需求。现在有一份名单记录各个队伍的ID和做出的题目数,需要你写一个程序,产生最终的排名。
为了简化题目,这里的排名规则为:做出题目数量多的队伍排在前面,如果题数相等,保持输入时的相对顺序不要改变。
Input
第一行包含一个正整数T( 1 ≤ T ≤ 15),表示有T组测试数据。每组数据第一行有一个正整数N(1 < N ≤ 10000),表示队伍数量。接下来N 行包含两个整数,1 ≤ ID ≤ 10^7, 0 ≤ M ≤ 100。ID为队伍的编号,M为做出的题数。
Output
每组数据输出包含N行,第i行有两个整数,ID和M表示排在第i位的队伍的ID和做出的题数。
Example Input
181 216 311 220 33 526 47 122 4
Example Output
3 526 422 416 320 31 211 27 1
Hint
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef struct student
{
int id;
int num;
int a;
}stu;
bool cmp(stu x,stu y)//此处为结构类型名
{
if(x.num!=y.num)return x.num>y.num;
if(x.a!=y.a)return x.a<y.a;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)//一个变量不重复使用
{
scanf("%d",&n);
stu std[n];//stu型数组std
for(i=0;i<n;i++)
{
scanf("%d%d",&std[i].id,&std[i].num);
std[i].a = i+1;//此处为标号处理,进行使用
}
sort(std,std+n,cmp);
for(i=0;i<n;i++)
{
if(i==0)
printf("%d %d",std[i].id,std[i].num);
else
printf("\n%d %d",std[i].id,std[i].num);
}
printf("\n");//注意多次输入的换行
}
return 0;
}
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef struct student
{
int id;
int num;
int a;
}stu;
bool cmp(stu x,stu y)//此处为结构类型名
{
if(x.num!=y.num)return x.num>y.num;
if(x.a!=y.a)return x.a<y.a;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)//一个变量不重复使用
{
scanf("%d",&n);
stu std[n];//stu型数组std
for(i=0;i<n;i++)
{
scanf("%d%d",&std[i].id,&std[i].num);
std[i].a = i+1;//此处为标号处理,进行使用
}
sort(std,std+n,cmp);
for(i=0;i<n;i++)
{
if(i==0)
printf("%d %d",std[i].id,std[i].num);
else
printf("\n%d %d",std[i].id,std[i].num);
}
printf("\n");//注意多次输入的换行
}
return 0;
}
Author
/*总结:
>1:
加深对结构体的使用
结构体数组
>2:
进一步使用冒泡法
over */
>1:
加深对结构体的使用
结构体数组
>2:
进一步使用冒泡法
over */