PAT:1012. The Best Rank (25) AC

#include<stdio.h>
#include<algorithm>
using namespace std;
struct Student
{
  int mID;
  int grade[4];              //0对应平均A,1对应C,2对应M,3对应E
}STU[2010];

char course[4]={‘A‘,‘C‘,‘M‘,‘E‘};      //所有的存储都对应ACME
int Rank[10000000][4]={0};          //每个学号四个成绩对应的排名
int now=0;                  //排序的时候按now这门课排
bool cmp(Student a,Student b)
{
  return a.grade[now]>b.grade[now];    //按照第now个成绩进行排序【warning】这里要按now成绩从大到小排列,写“<”勿写“>”
}
int main()
{
  int n,m;
  scanf("%d%d",&n,&m);          //n个输入,m个查询
  for(int i=0 ; i<n ; ++i)
  {
    scanf("%d%d%d%d",&STU[i].mID ,&STU[i].grade[1] ,&STU[i].grade[2] ,&STU[i].grade[3]);
    STU[i].grade[0]=(STU[i].grade[1]+STU[i].grade[2]+STU[i].grade[3])/3;
  }
  for(now=0 ; now<4 ; ++now)        //对于每一门课都按now进行排序,并将排名记录在Rank中
  {
    sort(STU,STU+n,cmp);
    Rank[STU[0].mID][now]=1;      //排序后结构体第一个为now标准下的第一名
    for(int i=1 ; i<n ;++i)
      if(STU[i].grade[now]==STU[i-1].grade[now])        //分数相同,并列排名【思维】:和前一个排名相同,不同则i+1
        Rank[STU[i].mID][now]=Rank[STU[i-1].mID][now];
      else
        Rank[STU[i].mID][now]=i+1;
  }
  int query;
  for(int i=0 ; i<m ; ++i)
  {
    scanf("%d",&query);
    if(Rank[query][0]==0)        //排名一定是1,2,3……,出现的时候,这个学号肯定有问题
      printf("N/A\n");
    else
    {
      int k=0;
      for(int j=0 ; j<4 ; ++j)
        if(Rank[query][j]<Rank[query][k])    //枚举这个学号的所有科目排名,最考前的保存在k中
          k=j;
      printf("%d %c\n",Rank[query][k],course[k]);
    }
  }
  return 0;
}
时间: 2024-08-25 07:30:44

PAT:1012. The Best Rank (25) AC的相关文章

[PTA] PAT(A) 1012 The Best Rank (25 分)

目录 Problem Solution Analysis Code Problem portal: 1012 The Best Rank (25 分) Solution Analysis ?一名学生有三门科目,以及计算出的一个平均成绩,每一个成绩都会有一个排名,现在想要让你输出某一个同学最高的排名(四种成绩排名的最高),以及对应的科目 ?如果给定的同学的四种课程排名的名次信息已经存在,那么就很简单,在里面找到最小的名次,以及对应的科目输出即可. ?可以创建四个数组,数组的每个元素存储某一门科目的

PAT Advanced 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 e

PAT:1060. Are They Equal (25) AC

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int n; string deal(string s,int &e) //[思维]1:吸收多余0:.2:找到“.”判断与1的大小.3:去除“.”的同时统计10的指数(正负与step2有关) { //4:判断是否删完了,删完了表明数字是0.5:存入前n个数字,不足用

PAT(A) 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 e

PAT:1086. Tree Traversals Again (25) AC

#include<stdio.h> #include<string.h> #include<stack> using namespace std; const int MAX=50; int n,cnt=0; //n个节点,cnt在后序输出的时候控制空格数量用 int PRE[MAX],IN[MAX]; //先序,中序 int preI,inI; //先序,中序的下标 stack<int> S; //栈 struct node { int data; nod

PAT:1009. Product of Polynomials (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> //[warning]double 输入%lf,输出%f struct arr { int exp; //指数 double cof; //系数 }arr[1005]; double ans[2010]; //下标是指数,内容是系数 int main() { memset(arr,0,sizeof(arr)); memset(ans,0,sizeof(ans)

PAT:1013. Battle Over Cities (25) AC

#include<stdio.h> #include<string.h> #include<vector> using namespace std; const int MAX=1010; int n,m,k; //城市数,高速公路数,查询次数 int DELETE; //要删除的点 vector<int> ADJ[MAX]; //邻接表 bool vis[MAX]; void DFS(int s) { if(DELETE==s) return; //表示该

PAT:1052. Linked List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int address; int data; int next; bool tag; }Node[100066]; bool cmp(node a,node b) { if(a.tag!=b.tag) return a.tag>b.tag; else return a.data<

PAT:1010. 一元多项式求导 (25) AC

#include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; int main() { int arr[2111]; fill(arr,arr+2111,0); int n=0,tmp; while(scanf("%d",&tmp)!=EOF) //存储系数和指数,下标从0开始偶数为系数,奇数为指数 arr[n++]=tmp; for(int i=0