PAT_甲级_1025 PAT Ranking (25分) (C++)【签到题】

目录

1,题目描述

题目大意

 输入

输出

2,思路

数据结构

3,代码


1,题目描述

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

题目大意

将多个测试点的成绩排名合并,显示总的排名、所属测试点、测试点的排名。

 输入

第一行:测试点数目N(<=100);

第二行:测试点1中所含测试者人数K(<=300);

接下来K行:测试者的num以及分数;

后一行:测试点2中所含测试者人数K(<=300);

……

输出

第一行:总人数

其余:输出所有人的num、总排名、所属测试点、测试点排名

2,思路

数据结构

  • 设计结构体struct node{string num;        int score, finalRank, localRank, localNum;}
  • vector<node> temp, ans:temp存放每个测试点的数据,并将每个测试点的数据加入ans中;

每接收一个测试点的数据就将它排序得出localRank,并将他们加入最终的ans中,最后将ans排序,得出finalRank。

3,代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct node{
    string num;
    int score, finalRank, localRank, localNum;
};
bool cmp(node a, node b){
    if(a.score > b.score)
        return true;
    else if(a.score == b.score){            //若成绩相同 按学号从低到高排序
        if(a.num < b.num) return true;      //字符串可以直接比较
    }
    return false;
}

int main(){
//#ifdef ONLINE_JUDGE
//#else
//    freopen("1.txt", "r", stdin);
//#endif

    int n, k, score;                        //n测试点数目 k各测试点中的人数 score分数
    string num;
    vector<node> temp, ans;
    node nod;
    cin>>n;
    for(int i = 1; i <= n; i++){
        cin>>k;
        for(int j = 1; j <= k; j++){
            cin>>num>>score;
            nod.num = num;
            nod.score = score;
            nod.localNum = i;
            temp.push_back(nod);
        }
        sort(temp.begin(), temp.end(), cmp);

        temp[0].localRank = 1;
        ans.push_back(temp[0]);
        for(int j = 1; j < temp.size(); j++){
            if(temp[j].score == temp[j-1].score)
                temp[j].localRank = temp[j-1].localRank;
            else
                temp[j].localRank = j + 1;

            ans.push_back(temp[j]);
        }
        temp.clear();
    }

    sort(ans.begin(), ans.end(), cmp);
    ans[0].finalRank = 1;

    for(int i = 1; i < ans.size(); i++){
        if(ans[i].score == ans[i-1].score)
            ans[i].finalRank = ans[i-1].finalRank;
        else
            ans[i].finalRank = i + 1;
    }

    cout<<ans.size()<<endl;
    for(int i = 0; i < ans.size(); i++){
        cout<<ans[i].num<<' '<<ans[i].finalRank<<' '<<ans[i].localNum<<' '<<ans[i].localRank<<endl;
    }

    return 0;
}
发布了45 篇原创文章 · 获赞 5 · 访问量 2175

猜你喜欢

转载自blog.csdn.net/qq_41528502/article/details/104303028