首页 > 其他分享 >PAT Advanced 1012. The Best Rank

PAT Advanced 1012. The Best Rank

时间:2023-06-18 14:34:14浏览次数:74  
标签:PAT Stu tempRank void int pStu1 Best student 1012

PAT Advanced 1012. The Best Rank

1. Problem Description:

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.

2. Input Specification:

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 CM and E. Then there are \(M\) lines, each containing a student ID.

3. Output Specification:

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.

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

5. Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

6. Performance Limit:

Code Size Limit
16 KB
Time Limit
200 ms
Memory Limit
64 MB

思路:

题目要求找出每个学生的最优排名,这里定义结构体Stu存储每个学生的相关信息,这里要注意下C++中结构体struct的使用与C中的不同之处。根据输入样例,主要有两点需要注意:

  • 平均分的计算是四舍五入后的结果。
  • 每一科目可能存在同分的情况,所以存在相同排名的情况。

根据题目描述,如果学生在不同的科目上有相同的最优排名,则按照A > C > M > E 的优先级进行输出。这里按照优先级顺序依次使用qsort()库函数对学生进行排序并记录各自的最优排名,最后按要求输出。具体来说,定义了指针数组pStu1用于排序,避免移动结构体本身进而节省了时间,最后查询时直接使用的遍历查找,没想到能满足时间要求。。。如果需要缩短时间的话还需要建立一个查找表。

第一次提交时testpoint2报wrong answer,检查了好几个小时找不出逻辑错误。无奈参考大佬题解:1012. The Best Rank (25)-PAT甲级真题_柳婼的博客-CSDN博客 ,瞬间醍醐灌顶,存储排名时存在逻辑错误,比如正确的排名应该是 1 1 3 4 5 而不是 1 1 2 3 4 ,真的好蠢的bug QAQ。这里代码写的比较冗余,简洁的代码可以参见大佬题解。

My Code & Result:

#include <iostream> // standard io
#include <cstdlib> // qsort header
#include <string> // string header
using namespace std;

struct Stu
{
    //char id[7];
    string id;
    int c;
    int m;
    int e;
    int avr;
    //double avr;
    int bestRank;
    char bestCourse;
};

int cmp_c(const void *p1, const void *p2);
int cmp_m(const void *p1, const void *p2);
int cmp_e(const void *p1, const void *p2);
int cmp_avr(const void *p1, const void *p2);

// first submit testpoint 2 wrong answer
int main(void)
{
    int totalNum=0, checkNum=0;
    int tempRank=0;
    string tempId;
    int rankCount=0; // this solve testpoint 2!!! The correct rank should be 1 1 3 4 5 instead of 1 1 2 3 4
    
    cin >> totalNum >> checkNum;

    if(!totalNum || !checkNum) return 0; // want this fixed testpoint 2, but failed
    
    Stu student[totalNum];
    
    Stu *pStu1[totalNum] = {0};
    
    for(int i=0; i<totalNum; ++i) // input info.
    {
        cin >> student[i].id >> student[i].c >> student[i].m >> student[i].e;
        student[i].avr = (student[i].c+student[i].m+student[i].e)*1.0/3 + 0.5;
        //student[i].avr = (student[i].c+student[i].m+student[i].e)*1.0/3;
        student[i].bestRank = -1;
        pStu1[i] = student + i; // assign the address of student
        
        // output info, test output
        // cout << student[i].id << " " << student[i].c << " " << student[i].m << " " << student[i].e << " " << student[i].avr << endl;
    }

    qsort(pStu1, totalNum, sizeof(Stu *), cmp_avr); // sort by avr
    tempRank = 1;
    rankCount = 0;
    for(int i=0; i<totalNum; ++i)
    {
        ++rankCount;
        if(i>0 && pStu1[i]->avr != pStu1[i-1]->avr) // update tempRank
        {
            // ++tempRank;
            tempRank = rankCount;
        }
        
        if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
        {
            pStu1[i]->bestRank = tempRank;
            pStu1[i]->bestCourse = 'A';
        }
        
    }
    
    //void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
    qsort(pStu1, totalNum, sizeof(Stu *), cmp_c); // sort by c program language
    tempRank = 1;
    rankCount = 0;
    for(int i=0; i<totalNum; ++i)
    {
        // test sort result
        //cout << pStu1[i]->id << " " << pStu1[i]->c << endl;

        ++rankCount;
        if(i>0 && pStu1[i]->c != pStu1[i-1]->c) // update tempRank
        {
            // ++tempRank;
            tempRank = rankCount;
        }
        
        if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
        {
            pStu1[i]->bestRank = tempRank;
            pStu1[i]->bestCourse = 'C';
        }

    }
    
    qsort(pStu1, totalNum, sizeof(Stu *), cmp_m); // sort by mathematics
    tempRank = 1;
    rankCount = 0;
    for(int i=0; i<totalNum; ++i)
    {
        // test sort result
        //cout << pStu1[i]->id << " " << pStu1[i]->m << endl;

        ++rankCount;
        if(i>0 && pStu1[i]->m != pStu1[i-1]->m) // update tempRank
        {
            // ++tempRank;
            tempRank = rankCount;
        }
        
        if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
        {
            pStu1[i]->bestRank = tempRank;
            pStu1[i]->bestCourse = 'M';
        }

    }

    qsort(pStu1, totalNum, sizeof(Stu *), cmp_e); // sort by english
    tempRank = 1;
    rankCount = 0;
    for(int i=0; i<totalNum; ++i)
    {
        ++rankCount;
        if(i>0 && pStu1[i]->e != pStu1[i-1]->e) // update tempRank
        {
            // ++tempRank;
            tempRank = rankCount;
        }
        
        if(pStu1[i]->bestRank == -1 || tempRank < pStu1[i]->bestRank) // update bestRank
        {
            pStu1[i]->bestRank = tempRank;
            pStu1[i]->bestCourse = 'E';
        }

    }

    for(int i=0; i<checkNum; ++i) // check rank & ouput
    {
        cin >> tempId;
        //cout << tempId << endl;
        int j=0;
        for(j=0; j<totalNum; ++j)
        {
            //string temp = student[j].id; // convert char * to string
            //if(tempId == temp)
            if(tempId == student[j].id)
            {
                cout << student[j].bestRank << " " << student[j].bestCourse << endl;
                break;
            }
        }
        if(j == totalNum) cout << "N/A" << endl;
    }


    return 0;
}

int cmp_c(const void *p1, const void *p2)
{
    // Stu *left = (Stu *)*p1; // here make mistake
    // Stu *right = (Stu *)*p2;
    Stu *left = *(Stu **)p1;
    Stu *right = *(Stu **)p2;
    
    return right->c - left->c;
}

int cmp_m(const void *p1, const void *p2)
{
    Stu *left = *(Stu **)p1;
    Stu *right = *(Stu **)p2;
    
    return right->m - left->m;
}

int cmp_e(const void *p1, const void *p2)
{
    Stu *left = *(Stu **)p1;
    Stu *right = *(Stu **)p2;
    
    return right->e - left->e;
}

int cmp_avr(const void *p1, const void *p2)
{
    Stu *left = *(Stu **)p1;
    Stu *right = *(Stu **)p2;
    
    return right->avr - left->avr;

    // if(right->avr > left->avr) return 1;
    // else if(right->avr < left->avr) return -1;
    // else return 0;
}
Compiler
C++ (g++)
Memory
712 / 65536 KB
Time
25 / 200 ms

标签:PAT,Stu,tempRank,void,int,pStu1,Best,student,1012
From: https://www.cnblogs.com/tacticKing/p/17489104.html

相关文章

  • unity将安卓streamingAssetsPath文件复制到persistentDataPath
    privatevoidTestCopy(){stringfrom=Application.streamingAssetsPath+"/Test/test.txt";stringto=Application.persistentDataPath+"/Test/";CopyFile(from,to);}publicstaticvoidCopyFile(stringsourcePath,stringdesti......
  • Redis特殊类型之Geospatial
    1.概述redis除了提供了五大基本数据类型String、List、Set、Hash、Zset,还有3个比较特殊的数据类型,Geospatial、Hyperloglog、Bitmap,这三个数据类型有一些比较有趣的应用场景,下面说说Geospatial,主要可以应用于跟地图相关的应用。朋友的定位,附近的人,打车距离计算?Redis的Geo在Redis......
  • 解决find命令报错: paths must precede expression
    解决find命令报错:pathsmustprecedeexpression 在一天早上,想在服务器/tmp目录清除一些pdf文件,大概一万多个文件,在执行命令的时候find/tmp-maxdepth1-mtime30-name*.pdf出现了错误:find:pathsmustprecedeexpressionUsage:find[-H][-L][-P][......
  • xpath定位
    Xpath定位1)、语法拆解//[@id="username"]其中//是dom节点的根节点;是指所有的元素;[]括号是将属性的键值对放入;@id是指属性为id的;后边的是赋值。name属性定位//*[@name="name_value"]这样的。type属性定位//*[@type="type_value"]。父/子元素定位//*[@class="f......
  • 迭代器模式(Iterator Pattern)
    迭代器模式(IteratorPattern)一、定义提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示。二、优缺点优点: 1、它支持以不同的方式遍历一个聚合对象。2、迭代器简化了聚合类。3、在同一个聚合上可以有多个遍历。4、在迭代器模式中,增加新的聚合类和......
  • Best Cow Fences(前缀和+特殊二分)
    之前的二分大多数都是整数类型的,今天又学到一种新型的二分,浮点数的二分,浮点数的二分可太巧妙了.且听我细细分说::OpenJudge-2018:BestCowFences#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+10;intn,k;doublea[N],s[N],l,r;boolcheck(doubleu)......
  • P2860 [USACO06JAN]Redundant Paths G 题解 tarjan边双连通分量
    题目链接:https://www.luogu.com.cn/problem/P2860题目大意:给定一个无向连通图,求至少加几条边,能使其变成一个边双连通图。解题思路:边双连通分量缩点后计算度数为\(1\)的节点个数,假设有\(cnt\)个,则答案为\((cnt+1)/2\)。示例程序:#include<bits/stdc++.h>usingnamespacestd;......
  • CF1205C Palindromic Paths 题解
    很好的一道题,思路自然,步骤清晰,结论好猜。唯一的缺点可能只是我赛时没有看到。构造可行解看到题目,也许我们很快就能想出一个做法:每次询问\((i,j,i+1,j)\),每行第一个额外询问\((i,j,i+1,j)\),这样询问总共\(n^2-1\)次即可。当我们怀疑看错题目重新检查时发现了被微软翻译......
  • PAT甲级笔记
    第一次刷题笔记如果对数组进行sort排序:sort(a,a+n,cmp1);如果对vectorv或者字符串v进行sort排序:sort(v.begin(),v.end(),cmp1);辗转相除法求最大公约数:1 intgcd(inta,intb){2 returnb==0?a:gcd(b,a%b);3 }#definemax(a,b)a逗号后面不要加空......
  • STUFF和FOR XML PATH('')
    初始状态:执行代码:SELECTSTUFF((SELECT','+Test_TableFROMdbo.Test_Table_MappingWHEREID=1570FORXMLPATH('')),1,1,'')text 显示结果 在SQLServer中,stuff()函数用于从源字符串中删除给定长度的字符序列,并从指定的起始索引插入给定的字符序列。STUFF(so......