博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 1012
阅读量:5757 次
发布时间:2019-06-18

本文共 4198 字,大约阅读时间需要 13 分钟。

1012. The Best Rank (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 Algrbra), 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 #include <stdio.h>
 2 #include <
string.h>
 3 #include <algorithm>
 4 
using 
namespace std;
 5 
 6 typedef 
struct Student{
 7     
char ID[
8];
 8     
double C,M,E,A;
 9 }Student;
10 
int search_rank(
double *,
double ,
int);
11 
int search_ID(Student *,
char *,
int );
12 
int main()
13 {
14     
double gradeC[
2000],gradeM[
2000],gradeE[
2000],gradeA[
2000];
15     Student student[
2000];
16     
int N,M,i;
17     
while(scanf(
"
%d%d
",&N,&M) != EOF){
18         
for(i=
0;i<N;++i){
19             scanf(
"
%s%lf%lf%lf
",student[i].ID,&student[i].C,&student[i].M,&student[i].E);
20             student[i].A = (student[i].C + student[i].M + student[i].E) / 
3;
21             gradeC[i] = student[i].C;
22             gradeM[i] = student[i].M;
23             gradeE[i] = student[i].E;
24             gradeA[i] = student[i].A;
25         }
26         sort(gradeC,gradeC+N);
27         sort(gradeM,gradeM+N);
28         sort(gradeE,gradeE+N);
29         sort(gradeA,gradeA+N);
30         
char searchID[
8];
31         
int rank,minRank;
32         
char minRankOrder;
33         
int ID_index;
34         
for(i=
0;i<M;++i){
35             scanf(
"
%s
",searchID);
36             ID_index = search_ID(student,searchID,N);
37             
if(ID_index < 
0){
38                 printf(
"
N/A\n
");
39                 
continue;
40             }
41             minRank = search_rank(gradeA,student[ID_index].A,N);
42             minRankOrder = 
'
A
';
43             
if(minRank == 
1){
44                 printf(
"
1 A\n
");
45                 
continue;
46             }
47             rank = search_rank(gradeC,student[ID_index].C,N);
48             
if(rank < minRank){
49                 minRank = rank;
50                 minRankOrder = 
'
C
';
51             }
52             
if(minRank == 
1){
53                 printf(
"
1 C\n
");
54                 
continue;
55             }
56             rank = search_rank(gradeM,student[ID_index].M,N);
57             
if(rank < minRank){
58                 minRank = rank;
59                 minRankOrder = 
'
M
';
60             }
61             
if(minRank == 
1){
62                 printf(
"
1 M\n
");
63                 
continue;
64             }
65             rank = search_rank(gradeE,student[ID_index].E,N);
66             
if(rank < minRank){
67                 minRank = rank;
68                 minRankOrder = 
'
E
';
69             }
70             printf(
"
%d %c\n
",minRank,minRankOrder);                
71         }
72     }
73     
return 
0;
74 }
75 
76 
int search_rank(
double *data,
double x,
int n)
77 {
78     
if(n <= 
0)
79         
return -
1;
80     
int i = n-
1;
81     
while(i >= 
0 && data[i--] != x);
82     
if(data[++i] == x)
83         
return n - i;
84     
else
85         
return -
1;
86 }
87 
int search_ID(Student *data,
char *ID,
int n)
88 {
89     
if(n <= 
0)
90         
return -
1;
91     
int i=
0;
92     
while(i < n && strcmp(data[i++].ID,ID));
93     
if(!strcmp(data[--i].ID,ID))
94         
return i;
95     
else
96         
return -
1;
97 }

 

posted on
2014-02-13 22:02 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/boostable/p/pat_1012.html

你可能感兴趣的文章
数据库自动化:DBA和DevOps的双赢
查看>>
Ruby 2.5.0概览
查看>>
Docker4Dev #7 新瓶装老酒 – 使用 Windows Container运行ASP.NET MVC 2 + SQLExpress 应用
查看>>
如何通过解决精益问题提高敏捷团队生产力
查看>>
阿里云数据库产品总监何云飞:云服务是影响未来10~20年的事
查看>>
Comment2Wechat —— Typecho 插件
查看>>
Apache下.htaccess文件配置及功能介绍
查看>>
Magento XML cheatsheet
查看>>
Egg 2.19.0 发布,阿里开源的企业级 Node.js 框架
查看>>
SegmentFault for Android —— 夜晚中的思考者
查看>>
sap的function module发布成web service后fm再次修改的处理 ...
查看>>
Kubernetes 弹性伸缩全场景解析 (四)- 让核心组件充满弹性 ...
查看>>
使用MySQLTuner-perl对MySQL进行优化
查看>>
ubuntu18.10手动安装mysql5.5
查看>>
SAP S/4HANA extensibility扩展原理介绍
查看>>
Swoole 4.1.0 正式版发布,支持原生 Redis/PDO/MySQLi 协程化 ...
查看>>
开发网络视频直播系统需要注意的地方
查看>>
haproxy mysql实例配置
查看>>
强化学习的未来— 第一部分
查看>>
掌握Python系统管理-调试和分析脚本1-debugging
查看>>