您的位置:首页 > 理论基础 > 数据结构算法

1012. The Best Rank (25)

2015-02-24 23:50 323 查看
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

/*-----------------------------------------------------------------
本题从数据结构的角度的讲没有什么难度,只是由于涉及科目不少,
书写过程比较繁琐。
1、对于所有的数据采用以id为关键字建立二叉查找树,以便查询方便。
(当然由于本题数据量不大,也可以采用链表的方式,查询时遍历)
2、由于各科的成绩范围固定,因此对于同一科目的排名可以按照计数排序
的思想,从而方便计算各科单科排名。
-------------------------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>

typedef struct node* Node;
struct imfor{
int id;
int program;
int math;
int english;
int average;
};/*一个学生的信息*/
struct node{
struct imfor this_node;
Node left;
Node right;
};/*树的节点内容*/

int min(int a,int b,int c,int d);//找出按照4种单科排序的方式得出的名次的最小值
void query(Node,int*,int*,int*,int*);//信息的查询,一共需要调用M次
Node build_the_tree(Node,Node);//把刚输入的信息插入到树

int main(){
int i,j,n,M;
int c[102]={0},m[102]={0},e[102]={0},a[102]={0};
Node root = NULL,temp;
scanf("%d%d",&n,&M);
for(i=0;i<n;i++){
temp = (Node)malloc(sizeof(struct node));
temp->left = temp->right = NULL;
scanf("%d%d%d%d", &temp->this_node.id, &temp->this_node.program, &temp->this_node.math, &temp->this_node.english);
temp->this_node.average = (temp->this_node.program + temp->this_node.math + temp->this_node.english)/3;
/*计数排序的初始数组*/
c[temp->this_node.program]++;
m[temp->this_node.math]++;
e[temp->this_node.english]++;
a[temp->this_node.average]++;
root = build_the_tree(root,temp);
}	//end of for loop
/*计数排序的最终数组*/
for(i=99;i>=0;i--){
c[i] = c[i+1] + c[i];
m[i] = m[i+1] + m[i];
e[i] = e[i+1] + e[i];
a[i] = a[i+1] + a[i];
}
for(i=0;i<M;i++)
query(root,a,c,m,e);
return 0;
}//end of main

/*找出a,b,c,d中的最小值,若a最小则返回1,b最小返回2,c最小返回3,d最小返回4*/
int min(int a,int b,int c,int d){
int min=a,result=1;
if(b<min){min = b ; result = 2;}
if(c<min){min = c ; result = 3;}
if(d<min){min = d ; result = 4;}
return result;
}

/*以id为关键字建立二叉查找树*/
Node build_the_tree(Node root ,Node temp ){
Node now;
if(root == NULL)
root = temp;
else{
now=root;
/*建立二叉树存放节点*/
while(1){
if(now->this_node.id > temp->this_node.id){
if(now->left)
now = now->left;
else {
now->left = temp;
break;
}
}
else if(now->this_node.id < temp->this_node.id){
if(now->right)
now = now->right;
else{
now->right = temp;
break;
}
}
}
}
return root;
}

/*信息的查询*/
void query(Node root,int *a,int*c,int *m, int *e){
int ranka,rankc,rankm,ranke,tempp,best;
Node temp;
scanf("%d",&tempp);
temp = root;
while(1){
if(temp->this_node.id > tempp)
if(temp->left)
temp = temp->left;
else{
printf("N/A\n");
break;
}
else if(temp->this_node.id < tempp)
if(temp->right)
temp = temp->right;
else{
printf("N/A\n");
break;
}
else if(temp->this_node.id == tempp){
ranka = a[temp->this_node.average+1] +1;
rankc = c[temp->this_node.program+1] +1;
rankm = m[temp->this_node.math+1] +1;
ranke = e[temp->this_node.english+1] +1;
best = min(ranka,rankc,rankm,ranke);
switch(best){
case 1:printf("%d A\n",ranka);break;
case 2:printf("%d C\n",rankc);break;
case 3:printf("%d M\n",rankm);break;
case 4:printf("%d E\n",ranke);break;
}
break;
}
}
return ;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息