1005 Graduate Admission 九度Online Judge

本文介绍了一个模拟高校招生过程的算法实现。通过定义学生和学校的数据结构,并使用快速排序进行高效排序,来完成模拟分配过程。该算法首先对学生按成绩排序,然后按照学生的志愿顺序尝试分配到学校,直到满足学校的名额限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cstdlib>

typedef struct Application
{
    int GI;
    int GE;
    int GF;
    int PS[6];
    int ID;
} Application;

typedef struct School
{
    int Quota;
    int count;
    int AppID[4001];
} School;

Application app[40001];
School school[101];
int cmp(const void *a, const void *b)
{
    struct Application *c = (Application *)a;
    struct Application *d = (Application *)b;
    if(c->GF != d->GF)
    {
        return d->GF - c->GF;
    }
    else if(c->GE != d->GE)
    {
        return d->GE - c->GE;
    }
}
int cmp2(const void *a,const void *b)
{
    return *(int *)a - *(int *)b;
}
int main ()
{
    int N,M,K,i,j,SID;
    while(scanf("%d %d %d",&N,&M,&K) != EOF)
    {
        for(i = 0; i < M; i++)
        {
            scanf("%d",&school[i].Quota);
            school[i].count = 0;
        }
        for(i = 0; i < N; i++)
        {
            scanf("%d %d",&app[i].GE,&app[i].GI);
            app[i].GF = (app[i].GE + app[i].GI) / 2;
            for(j = 0; j < K; j++)
            {
                scanf("%d",&app[i].PS[j]);
            }
            app[i].ID = i;
        }
        qsort(app,N,sizeof(app[0]),cmp);
        for(i = 0; i < N; i++)
        {
            for(j = 0; j < K; j++)
            {
                SID = app[i].PS[j];
                if(school[SID].Quota > 0)
                {
                    school[SID].AppID[school[SID].count] = i;
                    school[SID].count ++;
                    school[SID].Quota --;
                    break;
                }
                else
                {
                    int index = school[SID].AppID[school[SID].count - 1];
                    if(app[i].GF == app[index].GF && app[i].GE == app[index].GE)
                    {
                        school[SID].AppID[school[SID].count] = i;
                        school[SID].count ++;
                        school[SID].Quota --;
                        break;
                    }
                }
            }
        }
        for(i = 0; i < M; i++)
        {
            for(j = 0; j < school[i].count; j++)
            {
                school[i].AppID[j] = app[school[i].AppID[j]].ID;
            }
        }
        for(i = 0; i < M; i++)
        {
            if(school[i].count == 0)
            {
                printf("\n");
            }
            else if(school[i].count == 1)
            {
                printf("%d\n",school[i].AppID[0]);
            }
            else
            {
                qsort(school[i].AppID,school[i].count,sizeof(int),cmp2);
                int first = 1;
                for(j = 0; j < school[i].count; j++)
                {
                    if(first)
                    {
                        first = 0;
                    }
                    else
                    {
                        printf(" ");
                    }
                    printf("%d",school[i].AppID[j]);
                }
                printf("\n");
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值