PAT 1015 Graduate Admission (30)修改后

思路

  • 1.就是排好序然后逐个取出就好了,锻炼自己的码代码的效率就好了,以后做啥事都得提高效率呀!
  • 2.因为我这里sort排序后,储存的是i的话,最后输出是要对id进行排序,不方便,所以我就把id也存进去了,后面直接排序输出。

我出错的地方

  • 1.这种方式记住student(int x) :ge(0), gt(0), total(0), id(-1), choice(vector<int>(x, -1)){};
  • 2.这种声明方式也最好记住vector<student> students(n, student(p));
  • 3.记得要加constbool cmp(const student& a,const student &b);

代码

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

using namespace std;
//总体思路感觉还好,就是排好序然后逐个取出就好了,锻炼自己的码代码的效率就好了,以后做啥事都得提高效率呀
struct student
{
    int ge, gt, total, id;
    vector<int> choice;
    student() :ge(0), gt(0), total(0), id(-1), choice(0){};
    //这种方式记住
    student(int x) :ge(0), gt(0), total(0), id(-1), choice(vector<int>(x, -1)){};
};

struct school
{
    //因为我这里sort排序后,储存的是i的话,最后输出是要对id进行排序,不方便,所以我就把id也存进去了,后面直接排序输出。
    vector<int> stu,stu_id;
    int quota, lastrank;
    school() :stu(0), quota(0), lastrank(-1){};
    //school(int x) :stu(vector<int>(x, 0)), quota(0), lastrank(-1){};
};
//记得要加const
bool cmp(const student& a,const student &b)
{
    if (a.total > b.total || (a.total == b.total&&a.ge > b.ge))
        return true;
    else
        return false;
}
int main()
{
    ios::sync_with_stdio(false);
    int n, m, p;
    cin >> n>>m>> p;
    //这种声明方式也最好记住
    vector<student> students(n, student(p));
    vector<school> schools(m);
    for (int i = 0; i < m; i++)
    {
        cin >> schools[i].quota;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> students[i].ge >> students[i].gt;
        students[i].total = students[i].ge + students[i].gt;
        students[i].id = i;
        for (int j = 0; j < p; j++)
        {
            cin >> students[i].choice[j];
        }
    }
    sort(students.begin(), students.end(), cmp);
    /*for (int i = 0; i < n; i++)
    {
        cout << students[i].total << " " << students[i].ge;
        for (int j = 0; j < p; j++)
        {
            cout << " " << students[i].choice[j];
        }
        cout << endl;
    }*/
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < p; j++)
        {
            int ch = students[i].choice[j];
            //cout << "i:" << i << " ch:" << ch << endl;
            //我直接把他分为大于0有余额和余额为0俩种情况了
            if (schools[ch].quota>0)
            {
                schools[ch].stu.push_back(i);
                int idd = students[i].id;
                schools[ch].stu_id.push_back(idd);
                schools[ch].quota--;
                break;
            }
            else if (schools[ch].quota == 0)
            {
                if (!schools[ch].stu.empty())
                {
                    int num = (int)schools[ch].stu.size()-1;
                    int last = schools[ch].stu[num];
                    if (students[last].total == students[i].total&&students[last].ge == students[i].ge)
                    {
                        schools[ch].stu.push_back(i);
                        int idd = students[i].id;
                        schools[ch].stu_id.push_back(idd);
                        break;
                    }
                }
            }
        }
    }
    for (int i = 0; i < m; i++)
    {
        int s = schools[i].stu.size();
        sort(schools[i].stu_id.begin(), schools[i].stu_id.end());
        for (int j = 0; j < s; j++)
        {
            if (j == 0)
                cout << schools[i].stu_id[j];
            else
                cout << " " << schools[i].stu_id[j];
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值