PAT甲级 1080 Graduate Admission (测试点4段错误)

It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.

Each applicant will have to provide two grades: the national entrance exam grade GE​, and the interview grade GI​. The final grade of an applicant is (GE​+GI​)/2. The admission rules are:

  • The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.

  • If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE​. If still tied, their ranks must be the same.

  • Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one's turn to be admitted; and if the quota of one's most preferred shcool is not exceeded, then one will be admitted to this school, or one's other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.

  • If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.

Input Specification:

Each input file contains one test case.

Each case starts with a line containing three positive integers: N (≤40,000), the total number of applicants; M (≤100), the total number of graduate schools; and K (≤5), the number of choices an applicant may have.

In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.

Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant's GE​ and GI​, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M−1, and the applicants are numbered from 0 to N−1.

Output Specification:

For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants' numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

Sample Input:

11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4

Sample Output:

0 10
3
5 6 7
2 8

1 4

 分析:

一开始写的代码测试点4报段错误,先是排除了数组开得过小的情况,然后就找不出任何原因。网上的大佬们都说是cmp的问题,没有考虑ge也相等的情况,但是我这里判断了,所以更加懵。但是尝试了一下,发现确实就是出在cmp也就是compareGrade的问题,具体写法可以看下面的正确代码,该函数修改后也要对后面的学生比较是否破格录取的判断条件进行修改。(cmp函数造成的段错误是因为多了if判断?我不太清楚,希望评论区能有大佬给出答疑。)

这里还遇到过格式错误的问题,因为一行的末尾不能有多余的空格。

代码:

段错误代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct student{
	int ge;
	int gi;
	int zong;
	int school[6];
	int shunxu;
}s[40005];
bool compareGrade(student a,student b){
	if(a.zong!=b.zong)
	return a.zong>b.zong;
	if(a.ge!=b.ge) 
	return a.ge>b.ge;
	return true;//两者相等 
}
bool compareshunxu(student a,student b){
	return a.shunxu<b.shunxu;
}
bool compareSchool(student a,student b,int sch,int t){//t是该学校被放置的位置 
	int t1,t2;
	for(int i=0;i<sch;i++){
		if(a.school[i]==t){
			t1=i;
			break;
		}
	}
	for(int i=0;i<sch;i++){
		if(b.school[i]==t){
			t2=i;
			break;
		}
	}
	if(t1==t2)
	return true;
	return false;
} 
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	int quota[105];
	int luqu[105]={0};
	vector<student> xuexiao[105];
	for(int i=0;i<m;i++){
		cin>>quota[i];
	}
	for(int i=0;i<n;i++){
		cin>>s[i].ge>>s[i].gi;
		for(int j=0;j<k;j++){
			cin>>s[i].school[j];
		}
		s[i].zong=s[i].ge+s[i].gi;
		s[i].shunxu=i;
	}
	sort(s,s+n,compareGrade);
	for(int i=0;i<n;i++){
		for(int j=0;j<k;j++){
			int schooll=s[i].school[j];
			if(luqu[schooll]<quota[schooll]){
				xuexiao[schooll].push_back(s[i]);
				luqu[schooll]++;
				break;
			}else{
				if(compareGrade(s[i],xuexiao[schooll][luqu[schooll]-1])&&compareSchool(s[i],xuexiao[schooll][luqu[schooll]-1],m,schooll)){
					xuexiao[schooll].push_back(s[i]);
					luqu[schooll]++;
			    	break;
				}
			}
		}
	}
	for(int i=0;i<m;i++){
		sort(xuexiao[i].begin(),xuexiao[i].begin()+luqu[i],compareshunxu);
		if(xuexiao[i].size()>0){
			cout<<xuexiao[i][0].shunxu;
		} 
		for(int j=1;j<xuexiao[i].size();j++){
			cout<<" "<<xuexiao[i][j].shunxu;
		}
		cout<<endl;
	}
}

正确代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
struct student{
	int ge;
	int gi;
	int zong;
	int school[6];
	int shunxu;
}s[40005];
bool compareGrade(student a,student b){
	if(a.zong!=b.zong)
	return a.zong>b.zong;
	return a.ge>b.ge; 
}
bool compareshunxu(student a,student b){
	return a.shunxu<b.shunxu;
}
bool compareSchool(student a,student b,int sch,int t){//t是该学校的id,用于比较是不是两者的学校位置相同 
	int t1,t2;
	for(int i=0;i<sch;i++){
		if(a.school[i]==t){
			t1=i;
			break;
		}
	}
	for(int i=0;i<sch;i++){
		if(b.school[i]==t){
			t2=i;
			break;
		}
	}
	if(t1==t2)
	return true;
	return false;
} 
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	int quota[105];
	int luqu[105]={0};
	vector<student> xuexiao[105];
	for(int i=0;i<m;i++){
		cin>>quota[i];
	}
	for(int i=0;i<n;i++){
		cin>>s[i].ge>>s[i].gi;
		for(int j=0;j<k;j++){
			cin>>s[i].school[j];
		}
		s[i].zong=s[i].ge+s[i].gi;
		s[i].shunxu=i;
	}
	sort(s,s+n,compareGrade);
	for(int i=0;i<n;i++){
		for(int j=0;j<k;j++){
			int schooll=s[i].school[j];
			if(luqu[schooll]<quota[schooll]){//如果该校的名额没满,直接录取 
				xuexiao[schooll].push_back(s[i]);
				luqu[schooll]++;
				break;
			}else{
				//如果该校的名额已满,但是该生和该学校录取的最后一名是一样条件的,也可以录取 
				if((s[i].zong==xuexiao[schooll][luqu[schooll]-1].zong&&s[i].ge==xuexiao[schooll][luqu[schooll]-1].ge)
				&&compareSchool(s[i],xuexiao[schooll][luqu[schooll]-1],m,schooll)){
					xuexiao[schooll].push_back(s[i]);
					luqu[schooll]++;
			    	break;
				}
			}
		}
	}
	for(int i=0;i<m;i++){
		//输出要按原先学生的顺序大小排 
		sort(xuexiao[i].begin(),xuexiao[i].begin()+luqu[i],compareshunxu);
		if(xuexiao[i].size()>0){
			cout<<xuexiao[i][0].shunxu;
		} 
		for(int j=1;j<xuexiao[i].size();j++){
			cout<<" "<<xuexiao[i][j].shunxu;
		}
		cout<<endl;
	}
}
  • 30
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值