1. 程式人生 > >PA乙級 1085 PAT單位排行 (25 分)

PA乙級 1085 PAT單位排行 (25 分)

每次 PAT 考試結束後,考試中心都會發佈一個考生單位排行榜。本題就請你實現這個功能。

輸入格式:

輸入第一行給出一個正整數 N(≤105),即考生人數。隨後 N 行,每行按下列格式給出一個考生的資訊:

准考證號 得分 學校

其中准考證號是由 6 個字元組成的字串,其首字母表示考試的級別:B代表乙級,A代表甲級,T代表頂級;得分是 [0, 100] 區間內的整數;學校是由不超過 6 個英文字母組成的單位碼(大小寫無關)。注意:題目保證每個考生的准考證號是不同的。

輸出格式:

首先在一行中輸出單位個數。隨後按以下格式非降序輸出單位的排行榜:

排名 學校 加權總分 考生人數

其中排名是該單位的排名(從 1 開始);學校是全部按小寫字母輸出的單位碼;加權總分定義為乙級總分/1.5 + 甲級總分 + 頂級總分*1.5的整數部分;考生人數是該屬於單位的考生的總人數。

學校首先按加權總分排行。如有並列,則應對應相同的排名,並按考生人數升序輸出。如果仍然並列,則按單位碼的字典序輸出。

輸入樣例:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

輸出樣例:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

程式碼:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct{
	char school[7];
	int score_b,score_a,score_t,cnt_student,total_score;
}School;
int cmp_three(const void *a,const void *b){
	School *c=(School *)a;
	School *d=(School *)b;
	if(c->total_score!=d->total_score){
		return d->total_score - c->total_score;
	}
	else{
		if(c->cnt_student!=d->cnt_student) return  c->cnt_student - d->cnt_student;
		else return strcmp(c->school,d->school);
	}
}
int main(){
	int N,cnt_school=0;
	School l[100000];
	char id[7],school[7];
	int score;
	scanf("%d",&N);
	for(int i=0;i<N;++i){
		scanf("%s %d %s",id,&score,school);
		getchar();
		for(int j=0;j<strlen(school);++j){
			school[j]=tolower(school[j]);
		}
		if(cnt_school){
			int sign=0;
			for(int j=0;j<cnt_school;++j){
				if(strcmp(school,l[j].school)==0){
					sign=1;
					if(id[0]=='B') l[j].score_b+=score;
					if(id[0]=='A') l[j].score_a+=score;
					if(id[0]=='T') l[j].score_t+=score;
					++l[j].cnt_student;
					break;
				}
			}
			if(sign==0){
				strcpy(l[cnt_school].school,school);
				l[cnt_school].cnt_student=1;
				if(id[0]=='B') l[cnt_school].score_b=score,l[cnt_school].score_a=0,l[cnt_school].score_t=0;
				if(id[0]=='A') l[cnt_school].score_a=score,l[cnt_school].score_b=0,l[cnt_school].score_t=0;
				if(id[0]=='T') l[cnt_school].score_t=score,l[cnt_school].score_a=0,l[cnt_school].score_b=0;
				++cnt_school;
			}
		}
		else{
			strcpy(l[cnt_school].school,school);
			l[cnt_school].cnt_student=1;
			if(id[0]=='B') l[cnt_school].score_b=score,l[cnt_school].score_a=0,l[cnt_school].score_t=0;
			if(id[0]=='A') l[cnt_school].score_a=score,l[cnt_school].score_b=0,l[cnt_school].score_t=0;
			if(id[0]=='T') l[cnt_school].score_t=score,l[cnt_school].score_a=0,l[cnt_school].score_b=0;
			++cnt_school;
		}
	}
	for(int i=0;i<cnt_school;++i) l[i].total_score=l[i].score_b/1.5+l[i].score_a+l[i].score_t*1.5;
	qsort(l,cnt_school,sizeof(l[0]),cmp_three);
	printf("%d\n",cnt_school);
	int rank=1,cnt=1;
	for(int i=0;i<cnt_school;++i){
		printf("%d %s %d %d\n",rank,l[i].school,l[i].total_score,l[i].cnt_student);
		if(l[i].total_score!=l[i+1].total_score){
			rank=i+2;
		}
	}
	return 0;
}

在這裡插入圖片描述

出現的問題:

不出所料,有測試點執行超時,接下來就是對程式碼的優化了,打算採用折半查詢法,類似於1080。

第一次修改: