为什么不是所有测试用例都通过此问题?

时间:2019-05-29 07:47:26

标签: c++ sorting onlinejudge

我正在准备完成编程,PTA | 程序设计类实验辅助教学平台是一年半的问题。

我已经与其他人的答案进行了比较,但我仍然不知道我的代码有什么问题。 1080. Graduate Admission (30)-PAT甲级真题 – 柳婼 の blog中的代码可以完美传递。

问题主要通过排序解决,我应用了sort函数。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

struct Applicant
{
    int G_E; // GE
    int G_T; // GT
    int id;
    int sum; // sum = G_E + G_T
    int appliedSchools[7];
    int rank;
};

struct School
{
    int num = 0;
    int admitedApplicants[40010];
};

bool cmp (struct Applicant School_A, struct Applicant School_B)
{
    if (School_A.sum != School_B.sum)
        return School_A.sum > School_B.sum;
    else
        return School_A.G_E > School_B.G_E;
}

bool cmpID(int A, int B)
{
    if (A != B)
        return A < B;
}

int quotaOfGraduateSchools[100];
struct Applicant applicants[40000];

int main ()
{
    int N, M, K;
    scanf("%d %d %d", &N, &M, &K);

    for (int i = 0; i < M; ++i)
        scanf("%d", &quotaOfGraduateSchools[i]);

    for (int i = 0; i < N; ++i)
    {
        scanf("%d %d ", &applicants[i].G_E, &applicants[i].G_T);
        applicants[i].sum = applicants[i].G_E + applicants[i].G_T;
        applicants[i].id = i;
        for (int j = 0; j < K; ++j)
            scanf("%d", &applicants[i].appliedSchools[j]);
    }

    sort(applicants, applicants + N, cmp);
    applicants[0].rank = 1;
    for (int i = 1; i < N; ++i)
    {
        if (applicants[i].sum == applicants[i-1].sum && applicants[i].G_E == applicants[i-1].G_E)
            applicants[i].rank = applicants[i-1].rank;
        else
            applicants[i].rank = i + 1;
    }

    struct School schools[M];

//  for (int i = 0; i < N; ++i)
//      printf("Rank = %d, ID = %d, school_1 = %d, school_2 = %d, school_3 = %d\n", applicants[i].rank, applicants[i].id, applicants[i].appliedSchools[0], applicants[i].appliedSchools[1], applicants[i].appliedSchools[2]);

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < K; ++j)
        {
            if (schools[applicants[i].appliedSchools[j]].num < quotaOfGraduateSchools[applicants[i].appliedSchools[j]])
            {
                schools[applicants[i].appliedSchools[j]].admitedApplicants[schools[applicants[i].appliedSchools[j]].num] = applicants[i].id;
                ++schools[applicants[i].appliedSchools[j]].num;
                break;
            }
            else if (schools[applicants[i].appliedSchools[j]].num > 0 && applicants[schools[applicants[i].appliedSchools[j]].admitedApplicants[schools[applicants[i].appliedSchools[j]].num - 1]].rank == applicants[i].rank)
            {
                schools[applicants[i].appliedSchools[j]].admitedApplicants[schools[applicants[i].appliedSchools[j]].num] = applicants[i].id;
                ++schools[applicants[i].appliedSchools[j]].num;
                break;
            }
            else
                continue;
        }
    }

    for (int i = 0; i < M; ++i)
    {
        if (schools[i].num == 0)
            printf("\n");
        else
        {
            sort(schools[i].admitedApplicants, schools[i].admitedApplicants + schools[i].num, cmpID);
            printf("%d", schools[i].admitedApplicants[0]);
            for (int j = 1; j < schools[i].num; ++j)
                printf(" %d", schools[i].admitedApplicants[j]);
            printf("\n");
        }
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果ID相等->未定义的行为,

cmpID函数将不返回任何内容。打开编译器警告。

不要混用两种语言,struct Applicant School_A是C,在C ++中只是Applicant School_A,并且IO有coutcin。我不相信80个char宽的列,但是我仍然建议您进行一些重构。像auto& appliedSchoolI = applicants[i].appliedSchools[j]; auto& appliedSchool = schools[appliedSchoolI];...这样的小东西在这些if语句中将提供极大帮助: