zoukankan      html  css  js  c++  java
  • PAT 甲级 1012 The Best Rank

      1. 输出格式:排名  科目代号(优先按照排
        #include <iostream>
        #include <algorithm>
        #include <vector>
        using namespace std;
        const int INF = 0x3fffffff;
        int n,f;
        
        struct student
        {
            int rank[4];
            int no;
            int flag;//
            int score[4];//to store C,M,E,A
        };
        
        vector<student> stu;
        
        bool cmp1(student a,student b)//compare students score
        {
            return a.score[f]>b.score[f];
        }
        
        bool cmp2(student a,student b)//compare students id
        {
            return a.no<b.no;
        }
        
        void ranking()
        {
            stu[0].rank[f]=1;
            for (int i = 1; i < n; ++i) {
                stu[i].rank[f]=i+1;//
                if(stu[i].score[f]==stu[i-1].score[f])
                {
                    stu[i].rank[f]=stu[i-1].rank[f];//since the priorities of the ranking methods are ordered as A,C,M,E
                }
            }
        }
        
        
        
        
        void judge(int id,int &rank,int &index)
        {
            rank=INF;
            index=0;
            for (int i = 0; i < 4; ++i) {
                if (stu[id].rank[i]<rank)
                {
                    rank=stu[id].rank[i];
                    index = i;
                }
        
            }
        }
        
        
        int search(int no)
        {
            int l=0,h=n-1;
            while(l<=h)
            {
                int mid = (l+h)/2;
                if(stu[mid].no==no)
                {
                    return mid;
                }
                else if(stu[mid].no<no)
                {
                    l=mid+1;
                }
                else
                {
                    h=mid-1;
        
                }
            }
            return -1;
        }
        int main() {
           // std::cout << "Hello, World!" << std::endl;
           int m,no;
           char s[4]={'A','C','M','E'};//等级数组
           scanf("%d %d",&n,&m);//n for data lines,m for ask student ID
        
            for (int i = 0; i < n; ++i) {
            student a;
            scanf("%d",&no);//no for student id;
            a.flag=1;
            a.no=no;
            scanf("%d %d %d",&a.score[1],&a.score[2],&a.score[3]);
            a.score[0]=(a.score[1]+a.score[2]+a.score[3])/3;
            stu.push_back(a);
        
            }
        
            for ( f = 0; f < 4; ++f) {
                sort(stu.begin(),stu.end(),cmp1);//descending order by score
                ranking();//just sort by rank key
            }
        
            sort(stu.begin(),stu.end(),cmp2);//ascending order by no
        
            for (int i = 0; i < m; ++i) {
                scanf("%d",&no);
                int rank = INF,index=0;
                int x=search(no);//bisection method find no
                 if(x==-1)
                 {
                     printf("N/A
        ");
                 }
                 else
                 {
                     judge(x,rank,index);//find the min rank and it's index
                     printf("%d %c
        ",rank,s[index]);
                 }
        
            }
        
            return 0;
        }
        View Code
        名靠前科目,最优科目若与他人同级,则按照ACME顺序)
  • 相关阅读:
    【高软作业4】:Tomcat 观察者模式解析 之 Lifecycle
    Eclipse 导入 Tomcat 源码
    【高软作业3】:原型化系统 DevTools
    Java Obejct
    Java PriorityQueue
    【高软作业2】:Java IDE调研分析
    GitHub fork 合作开发 快速实现版
    用C#实现天气预报(调用WebService)
    hover和点击事件之间的冲突
    BurpSuite2021系列(三)新建扫描
  • 原文地址:https://www.cnblogs.com/jeseesmith/p/13976886.html
Copyright © 2011-2022 走看看