pat 甲级 1025 PAT Ranking

其实思路都对了
处理的还是不好
没必要都在一个里面存然后排
为了解决一个麻烦而制造更多麻烦
这是第一版麻烦的夭折代码

要记住排名处理的方法

if(arr[p].score == arr[p-1].score) arr[p].rank = arr[p-1].rank;
else arr[p].rank = p+1;
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

struct stu{

    int score;
    string name;
    int rankout;
    int runkin;
    int part;

};

stu student[310];
int n, k[310], sum;

bool myscore(stu a, stu b){
    return a.score > b.score;
}

int main(){

    scanf("%d",&n);
    int s = 0;
    int first = 0, end = 0;
    int ra = 0, nk;
    for(int i=1; i<=n; i++){
        scanf("%d",&k[i]);
        sum += k[i];

        for(int j=0; j<k[i]; j++){
            cin >> student[s].name >> student[s].score;
            student[s].part = i;
            //cout << student[j].name << " eeee " << s << endl;
            s++;

        }
        nk += k[i]; 
        end += k[i];
        sort(student+first,student+end,myscore);
        first = end + 1;
        student[ra].runkin = 1;
        int p;
        for(p=ra+1; p<nk; p++){
            if(student[p].score==student[p-1].score) student[p].runkin = student[p-1].runkin;
            else student[p].runkin = p+1-ra;
            //cout << student[p].runkin <<  "rankin " << student[p].score << " " << p <<endl; 
        }
        for(int l=ra; l<=nk; l++){
            cout << student[l].name << " " << student[l].part  << " " << student[l].runkin<< endl; 
        }
        ra += k[i]; 
        p = 0;
        cout << ra << "ra " << endl;

        //cout << first << "first  " << end << endl;
    }

    cout << sum << endl;
    sort(student,student+sum,myscore);
    for(int l=0; l<sum; l++){
        cout << student[l].name << " " << student[l].part  << " " << student[l].runkin<< endl;
    }


    return 0;
} 
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
struct student {
    long long int no;
    int score, finrank, loca, locarank;
};
bool cmp1(student a, student b) {
    return a.score != b.score ? a.score > b.score : a.no < b.no;
}
int main() {
    int n, m;
    scanf("%d", &n);
    vector<student> fin;
    for(int i = 1; i <= n; i++) {
        scanf("%d", &m);
        vector<student> v(m);
        for(int j = 0; j < m; j++) {
            scanf("%lld %d", &v[j].no, &v[j].score);
            v[j].loca = i;
        }
        sort(v.begin(), v.end(), cmp1);
        v[0].locarank = 1;
        fin.push_back(v[0]);
        for(int j = 1; j < m; j++) {
            v[j].locarank = (v[j].score == v[j - 1].score) ? (v[j - 1].locarank) : (j + 1);
            fin.push_back(v[j]);
        }
    }
    sort(fin.begin(), fin.end(), cmp1);
    fin[0].finrank = 1;
    for(int j = 1; j < fin.size(); j++)
        fin[j].finrank = (fin[j].score == fin[j - 1].score) ? (fin[j - 1].finrank) : (j + 1);
    printf("%d\n", fin.size());
    for(int i = 0; i < fin.size(); i++)
        printf("%013lld %d %d %d\n", fin[i].no, fin[i].finrank, fin[i].loca, fin[i].locarank);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mdzz_z/article/details/81515493
今日推荐