用LUA(和C++)刷PAT (Advanced Level) ——1080 Graduate Admission

这段代码实现了一个模拟大学录取过程的程序。它首先读取学生信息和学校配额,然后根据学生的总成绩进行排序。接着,按照成绩优先级分配学生到他们首选的学校,直到所有名额分配完毕。最后,输出每个学校录取的学生ID。程序使用了C++标准库中的排序和数据结构功能。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

struct student{
    int id, Ge, Gi;
    int choices[5];
};

bool compSt(student a, student b){
    if(a.Ge + a.Gi != b.Ge + b.Gi)
        return a.Ge + a.Gi > b.Ge + b.Gi;
    return a.Ge > b.Ge;
}
    
bool compAp(int a, int b){
    return a < b;
}

int main()
{
    int N, M, K;
    int quotas[100];
    cin>>N>>M>>K;
    vector<vector<int>> applicants(M);//记录每个学校的录取信息
    vector<student> applications(N);
    for(int i = 0; i < M; i++)
        scanf("%d", &(quotas[i]));
    for(int i = 0; i < N; i++){
        scanf("%d %d", &(applications[i].Ge), &(applications[i].Gi));
        applications[i].id = i;
        for(int j = 0; j < K; j++)
            scanf("%d", &((applications[i].choices)[j]));
    }
    sort(applications.begin(), applications.end(), compSt);
    bool tie_record[100] = {false};
    int tie_Gt = -1, tie_Ge = -1;
    for(auto app = applications.begin(); app != applications.end(); app ++){
        if(app -> Ge + app -> Gi != tie_Gt || app -> Ge != tie_Ge){
            memset(tie_record, false, sizeof(bool) * 100);
            tie_Gt = app -> Ge + app -> Gi;
            tie_Ge = app -> Ge;
        }
        for(int i = 0; i < K; i++){
            if(tie_record[app->choices[i]] || quotas[app->choices[i]] > 0){
                quotas[app->choices[i]] --;
                tie_record[app->choices[i]] = true;
                applicants[app->choices[i]].push_back(app->id);
                break;
            }
        }
    }
    for(auto school = applicants.begin(); school != applicants.end(); school ++){
        sort(school->begin(), school->end(), compAp);
        for(auto itr = school->begin(); itr != school->end(); itr++){
            printf("%d", *itr);
            if(school->end() - itr > 1)
                printf(" ");
        }
        printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值