zoukankan      html  css  js  c++  java
  • PAT 1012

    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 ");
    39                 continue;
    40             }
    41             minRank = search_rank(gradeA,student[ID_index].A,N);
    42             minRankOrder = 'A';
    43             if(minRank == 1){
    44                 printf("1 A ");
    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 ");
    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 ");
    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 ",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 }
  • 相关阅读:
    域名证书文件包含两段证书
    关于docker的scratch镜像与helloworld
    JS实现网站内容的禁止复制和粘贴、另存为
    使用chromedriver实现豆瓣网页的全网页截图
    Python Tornado初学笔记之数据库(二)
    Python Tornado初学笔记之表单与模板(一)
    Redis中sds 模块的 API
    原码、补码、反码、移码之间的换算
    Python中的位运算
    Python中进制的转换
  • 原文地址:https://www.cnblogs.com/boostable/p/pat_1012.html
Copyright © 2011-2022 走看看