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 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


这题也卡了很久,不难,但实现起来复杂,其实代码中我用了很多次暴力地顺利搜索,当时很担心超时,最后居然没有。

学科数组subject[][2]这个利用我考虑了很久,写的时候觉得很乱,现在回头看又觉得思路挺清晰的


#include <stdio.h>
#include <stdlib.h>

typedef struct student STD;
struct student
{
    int IDty;
    int grade[4]; /**< 存放各科成绩 */
    int Rank[4]; /**< 存放各科成绩的排名 */
};

/**< 将某一科目进行排序 */
void sortion(int ranking[][2], int numOfStd);
void sortion(int ranking[][2], int numOfStd)
{
    int i, j;

    int tmpV, tmpID;
    for(i=0; i<numOfStd; i++)
    {
        for(j=0; j<numOfStd-i-1; j++)
        {
            if(ranking[j][1] < ranking[j+1][1])
            {
                tmpV = ranking[j+1][1];
                tmpID = ranking[j+1][0];

                ranking[j+1][1] = ranking[j][1];
                ranking[j+1][0] = ranking[j][0];

                ranking[j][1] = tmpV;
                ranking[j][0] = tmpID;
            }
        }
    }
}

/**< 找出id为target的学生在ranking这个科目的排名 */
int findRank(int ranking[][2], int numOfStd, int target);
int findRank(int ranking[][2], int numOfStd, int target)
{
    int i;
    for(i=0; i<numOfStd; i++)
    {
        //printf("target:%d finding:%d\n", target, ranking[i][0]);
        if(ranking[i][0] == target)
        {

            return i;
        }
    }
    return -1;
}

/**< 找出id为target的学生在mySTD数组里的下表 */
int findStd(STD mySTD[], int numOfStd, int target);
int findStd(STD mySTD[], int numOfStd, int target)
{

    int i;
    for(i=0; i<numOfStd; i++)
    {
        if(mySTD[i].IDty == target)
        {
            return i;
        }
    }
    return -1;
}

int main()
{
    int i, j;

    char subject[4] = {'A', 'C', 'M', 'E'};/**< 0:A, 1:C, 2:M, 3:E*/

    int numOfStd, numOfCker;

    scanf("%d %d", &numOfStd, &numOfCker);

    STD mySTD[numOfStd];

    /**< 各科目的数组,0存id,1存分数 */
    int Averge[numOfStd][2];
    int Math[numOfStd][2];
    int CPL[numOfStd][2];
    int Eng[numOfStd][2];

    /**< 输入部分,顺便计算平均分,以及将分数录入科目数组 */
    for(i=0; i<numOfStd; i++)
    {
        scanf("%d %d %d %d", &mySTD[i].IDty, &mySTD[i].grade[1], &mySTD[i].grade[2], &mySTD[i].grade[3]);
        /**< 计算平均分 */
        mySTD[i].grade[0] = (mySTD[i].grade[1] + mySTD[i].grade[2] + mySTD[i].grade[3])/3;
        mySTD[i].Rank[4] = 0;

        /**< 科目数组的数据录入 */
        Averge[i][0] = mySTD[i].IDty;
        Averge[i][1] = mySTD[i].grade[0];

        CPL[i][0] = mySTD[i].IDty;
        CPL[i][1] = mySTD[i].grade[1];

        Math[i][0] = mySTD[i].IDty;
        Math[i][1] = mySTD[i].grade[2];

        Eng[i][0] = mySTD[i].IDty;
        Eng[i][1] = mySTD[i].grade[3];

        //printf("%d A:%d C:%d M:%d E:%d\n", mySTD[i].IDty, mySTD[i].grade[0], mySTD[i].grade[1], mySTD[i].grade[2], mySTD[i].grade[3]);
    }

    sortion(Averge, numOfStd);
    sortion(CPL, numOfStd);
    sortion(Math, numOfStd);
    sortion(Eng, numOfStd);

    /**< 将各科成绩排名录入mySTD中的rank数组 */
    for(i=0; i<numOfStd; i++)
    {

        int targetID = mySTD[i].IDty;
        int tmpRank = findRank(Averge, numOfStd, targetID); /**< 找出该学生在平均分中的排名 */
        /**< 如果排名不是第一名,则比较和上一名的分数是不是相同 */
        while((tmpRank > 0) && (Averge[tmpRank][1] == Averge[tmpRank-1][1]))
        {
            tmpRank--;
        }
        mySTD[i].Rank[0] = tmpRank;

        tmpRank = findRank(CPL, numOfStd, targetID);
        while(tmpRank > 0 && CPL[tmpRank][1] == CPL[tmpRank-1][1])
        {
            tmpRank--;
        }
        mySTD[i].Rank[1] = tmpRank;

        tmpRank = findRank(Math, numOfStd, targetID);
        while(tmpRank > 0 && Math[tmpRank][1] == Math[tmpRank-1][1])
        {
            tmpRank--;
        }
        mySTD[i].Rank[2] = tmpRank;

        tmpRank = findRank(Eng, numOfStd, targetID);
        while(tmpRank > 0 && Eng[tmpRank][1] == Eng[tmpRank-1][1])
        {
            tmpRank--;
        }
        mySTD[i].Rank[3] = tmpRank;

        //printf("%d RANK: %d %d %d %d\n", targetID, mySTD[i].Rank[0], mySTD[i].Rank[1], mySTD[i].Rank[2], mySTD[i].Rank[3]);

    }

    /**< 输出结果部分 */
    for(i=0; i<numOfCker; i++)
    {
        /**< 输出格式控制 */
        if(i!=0)
        {
            printf("\n");
        }

        int checker;
        scanf("%d", &checker);
        /**< 寻找该id的学生,找到就NA */
        int target = findStd(mySTD, numOfStd, checker);

        if(target == -1)
        {
            printf("N/A");
        }
        else
        {
            /**< 找出该学生各科排名最靠前的那科 */
            int topRank = 0;
            for(j=0; j<4; j++)
            {
                if(mySTD[target].Rank[topRank] > mySTD[target].Rank[j])
                {
                    topRank = j;
                }
            }

            printf("%d %c", mySTD[target].Rank[topRank]+1, subject[topRank]);
        }
    }
    system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值