1. 程式人生 > >PAT (Advanced Level) Practice 1012 The Best Rank (25)(25 分)

PAT (Advanced Level) Practice 1012 The Best Rank (25)(25 分)

1012 The Best Rank (25)(25 分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output "N/A".

Sample Input

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output

1 C
1 M
1 E
1 A
3 A
N/A
作者: CHEN, Yue 單位: PAT聯盟 時間限制: 400ms 記憶體限制: 64MB 程式碼長度限制: 16KB
題解1: 剛開始沒看懂題意,以為是每個人選出各自的最高分輸出。後來發現是每個科目都需要排序,記錄排名,最後比較得出每個人排名最高的科目和排名。

剛開始採用的方法是這樣的:使用struct型別記錄個人資訊,包括編號、各科成績和平均成績。然後使用sort函式對各科進行排名。最後查詢每個輸入的編號,再找到他的排名最高的科目和排名輸出。

這個方法一直超時,又改成了把每個編號在結構體陣列中的位置記錄下來,用空間換時間。

原始碼1:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

char sub[5] = { 'A','C','M','E' };
int i;
struct student
{
	int num;
	int score[4], rank[4];
};

bool cmp(student a, student b)
{
	return a.score[i] > b.score[i];
}
int main()
{
	int m, n;
	int j, k, temp;
	int best;
	student stu[2005];
	int exist[1000000] = { 0 };
	cin >> n >> m;
	for (i = 0; i < n; i++)
	{
		scanf("%d %d %d %d", &stu[i].num, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
		stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
	}
	for (i = 0; i < 4; i++)
	{
		sort(stu, stu + n, cmp);
		stu[0].rank[i] = 1;
		for (j = 1; j < n; j++)
		{
			stu[j].rank[i] = j + 1;
			if (stu[j].score[i] == stu[j - 1].score[i])
				stu[j].rank[i] = stu[j - 1].rank[i];
		}
	}
	for (j = 0; j < n; j++)
	{
		exist[stu[j].num] = j + 1;
	}
	for (j = 0; j < m; j++)
	{
		cin >> temp;
		if (exist[temp] != 0)
		{
			i = exist[temp] - 1;
			best = 0;
			for (k = 1; k < 4; k++)
				if (stu[i].rank[k] < stu[i].rank[best])
					best = k;
			printf("%d %c\n", stu[i].rank[best], sub[best]);
		}
		else
			printf("N/A\n");
	}
	return 0;
}

題解2:

使用 map 進行查詢。

原始碼2:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
char sub[5] = { 'A','C','M','E' };
int i;
struct student
{
	int num;
	int score[4], rank[4];
};
bool cmp(student a, student b)
{
	return a.score[i] > b.score[i];
}
int main()
{
	int m, n;
	int j, k, temp;
	int best;
	student stu[2005];
	map<int,int>match;
	cin >> n >> m;
	for (i = 0; i < n; i++)
	{
		scanf("%d %d %d %d", &stu[i].num, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
		stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;
	}
	for (i = 0; i < 4; i++)
	{
		sort(stu, stu + n, cmp);
		stu[0].rank[i] = 1;
		for (j = 1; j < n; j++)
		{
			if (stu[j].score[i] == stu[j - 1].score[i])
				stu[j].rank[i] = stu[j - 1].rank[i];
			else 			stu[j].rank[i] = j + 1;
		}
	}
	for (j = 0; j < n; j++)
	{
		match[stu[j].num] = j + 1;
	}
	for (j = 0; j < m; j++)
	{
		cin >> temp;
		if (match[temp] != 0)
		{
			i = match[temp] - 1;
			best = 0;
			for (k = 1; k < 4; k++)
				if (stu[i].rank[k] < stu[i].rank[best])
					best = k;
			printf("%d %c\n", stu[i].rank[best], sub[best]);
		}
		else
			printf("N/A\n");
	}
	return 0;
}

注意事項:

1、在比較函式 com() 中,如果有未知引數是無法編譯成功的,必須把引數在 com() 前定義了。

2、排名方法:如果n人同分,則排名相同,後面的人排名要加n,不是加1。

3、超時問題:使用新的陣列記錄各學生編號的位置,或者使用 map 。

4、在一個程式碼中如果有巢狀迴圈,一定要搞清楚 i,j,k 的關係,千萬不要出現 for(i=0;i<n;j++) 這樣低階的錯誤!!!這個錯誤一直沒找到,浪費了大量時間。如果迴圈太多,最好是用其他有具體含義的比如flag,num,count這樣的名稱替代。