PAT 1015 Graduate Admission (30)

这道题谁帮我找出那个bug,好想请那个人吃饭,有俩分没通过。

思路

  • 1.首先把俩个结构体的元素想好,比如多一个rank,感觉比较的话就方便很多。
  • 2.quota配额,囧….
  • 3.总的来说就是先排好序,然后看学校满没满以及lastrank取人就是了。

我出错的一些点

  • 1.难道不能用>?,<可以用,是sort默认是从小到大的原因吗?
  • 2.bool cmp(const …,const …),不加const报错。
  • 3.vector<…> name(n,…),这样声明n个比较方便。
  • 4.(int).size()这个是咋回事啊,有时候为啥不用这么些?

代码

#include <iostream>
#include <vector>
#include <math.h>
#include<algorithm>

using namespace std;
//quota配额
struct application_
{
    //多一个rank要方便很多
    int id,GE,GI,total,rank ;
    vector<int> choice;
    application_() :id(-1), GE(0), GI(0), total(0), rank(-1), choice(0){};
    application_(int x) :id(-1), GE(0), GI(0), total(0), rank(-1), choice(vector<int>(x, 0)){};
    //难道不能用>?,<可以用
    /*bool operator < ( application_ r) {
        if (total < r.total)
            return true;
        else
            return false;
    }*/
};
//不加const报错
bool cmp(const application_& a,const application_& b)
{
    if (a.total > b.total)
        return true;
    else if (a.GE == b.GE&&a.total > b.total)
        return true;
    //else要写完
    else return false;
}
struct school
{
    int quato;
    int lastRank;
    vector<int> student;
    school() :quato(0), lastRank(0), student(0){};

};

int main()
{
    ios::sync_with_stdio(false);
    int n, m, k;
    cin >> n >> m >> k;
    //这样声明感觉爽很多
    vector<application_> students(n, application_(k));
    vector<school> schools(m);
    for (int i = 0; i < m; i++)
    {
        cin >> schools[i].quato;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> students[i].GE >> students[i].GI;
        students[i].id = i;
        students[i].total = students[i].GE + students[i].GI;
        for (int j = 0; j < k; j++)
        {
            cin >> students[i].choice[j];
        }
    }
    sort(students.begin(), students.end(),cmp);

    students[0].rank = 0;
    for (int i = 1; i < n; i++)
    {
        if ( (students[i].total == students[i - 1].total&&students[i].GE == students[i - 1].GE))
            students[i].rank = students[i-1].rank;
        else
            //students[i].rank = students[i-1].rank+1;
            students[i].rank = i;
    }
    int ch_id;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < k; j++)
        {
            ch_id = students[i].choice[j];
            if (schools[ch_id].quato > 0||(schools[ch_id].quato==0&&schools[ch_id].lastRank==students[i].rank))
            {
                schools[ch_id].student.push_back(students[i].id);
                if (schools[ch_id].quato > 0)
                {
                    schools[ch_id].quato--;
                }
                schools[ch_id].lastRank = students[i].rank;

                //记得录取了就break。
                break;
            }
        }
    }
    for (int i = 0; i < m; i++)
    {
        sort(schools[i].student.begin(), schools[i].student.end());
        //注意(int).size()
        for (int j = 0; j < (int)schools[i].student.size(); j++)
        {
            //cout << "j:" << j<<endl;
            if (j == 0)
                cout << schools[i].student[j];
            else
                cout <<  " " <<schools[i].student[j];
        }
        cout << endl;
    }
    /*for (int i = 0; i < n; i++)
    {
        cout << "id:" << students[i].id << " total:" << students[i].total << " GE:" << students[i].GE << " rank:" << students[i].rank << endl;
    }*/
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值