SDUT 2446 最终排名

最终排名

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和做出的题数。

Sample Input
1
8
1 2
16 3
11 2
20 3
3 5
26 4
7 1
22 4

Sample Output
3 5
26 4
22 4
16 3
20 3
1 2
11 2
7 1

注意本题中的排序不能用sort()函数,因为STL库中的sort函数是用快速排序写的,对数组有破坏
性(即前后两个值的大小相同时可能会改变其次序),因此直接使用冒泡排序即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef struct Team
{
    int ID;
    int counts;
}team[10010];

bool cmp(Team a,Team b)
{
    return a.counts>b.counts;
}

int main()
{
    int  t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        Team team[10010],temp;
        for(int i = 0;i<n;i++)
        {
            cin>>team[i].ID>>team[i].counts;
        }
        //sort(team,team+n,cmp);    //注意:不能直接用sort函数
        for(int j=0;j<n-1;j++)
            for(int k=0;k<n-1-j;k++)
                if(team[k].counts<team[k+1].counts)
                {
                    temp=team[k];
                    team[k]=team[k+1];
                    team[k+1]=temp;
                }
        for(int i = 0;i<n;i++)
        {
            cout<<team[i].ID<<" "<<team[i].counts<<endl;
        }
    }
    return 0;
}
posted @ 2019-04-30 16:49  Hu_YaYa  阅读(32)  评论(0)    收藏  举报