首页 > 试题广场 >

The Best Rank (25)

[编程题]The Best Rank (25)
  • 热度指数:4470 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
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".
示例1

输入

5 6<br/>310101 98 85 88<br/>310102 70 95 88<br/>310103 82 87 94<br/>310104 91 91 91<br/>310105 85 90 90<br/>310101<br/>310102<br/>310103<br/>310104<br/>310105<br/>999999

输出

1 C<br/>1 M<br/>1 E<br/>1 A<br/>3 A<br/>N/A
ID is a string of 6 digits,但测试case中冒出了字符
发表于 2017-01-22 14:09:49 回复(0)

注意点

1.ID必须是字符串类型,不能用整数存。
2.求平均分时直接向下取整,而不是四舍五入。

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int w = 0; 
struct stu{
    string id;
    int c, m, e, a;
    int cc, mm, ee, aa; //排名 
}E[2005];

bool cmp(stu a, stu b){
    if (w == 0) return a.a > b.a;  
    else if(w == 1) return a.c > b.c;
    else if(w == 2) return a.m > b.m;
    else return a.e > b.e;
}
void print(int x){
    char ch = 'A';
    int d = E[x].aa;
    if(d > E[x].cc) d = E[x].cc, ch = 'C'; 
    if(d > E[x].mm) d = E[x].mm, ch = 'M';
    if(d > E[x].ee) d = E[x].ee, ch = 'E';
    cout<<d<<" "<<ch<<endl;
}
int main(){
    int n, m, x;
    string s;
    unordered_map<string, int> mp;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin>>E[i].id >>E[i].c >> E[i].m>>E[i].e;
        E[i].a = (E[i].c + E[i].m + E[i].e)/3.0;
    }
    sort(E, E + n, cmp);
    for(int i = 0 ; i < n; i++){ //相同成绩相同排名 
        if(i > 0 && E[i-1].a == E[i].a) E[i].aa = E[i-1].aa;
        else E[i].aa = i + 1;
    } 
    w=1;
    sort(E, E + n, cmp);
    for(int i = 0 ; i < n; i++){
        if(i > 0 && E[i-1].c == E[i].c) E[i].cc = E[i-1].cc;
        else E[i].cc = i + 1;
    } 
    w=2;
    sort(E, E + n, cmp);
    for(int i = 0 ; i < n; i++){
        if(i > 0 && E[i-1].m == E[i].m) E[i].mm = E[i-1].mm;
        else E[i].mm = i + 1;
    } 
    w=3;
    sort(E, E + n, cmp);
    for(int i = 0 ; i < n; i++){
        if(i > 0 && E[i-1].e == E[i].e) E[i].ee = E[i-1].ee;
        else E[i].ee = i + 1;
        mp[E[i].id] = i + 1; 
    } 
    while(m--){
        cin >> s;
        if(!mp[s]) cout<<"N/A"<<endl;
        else{
            print(mp[s] - 1);
        } 
    }
    return 0;
} 
发表于 2019-08-18 16:33:00 回复(2)
#include <cstdio>
#include <algorithm>
using namespace std;

struct student{
    int id;
    int score[4];       //存放4个分数
}s[2010];
int Rank[10000000][4]={0};  //Rank[id][0]到Rank[id][3] 为4门课对应的排名
int q;  //在cmp函数中使用,表示当前按p号分数排序s数组
bool cmp(student a,student b){
    return a.score[q]>b.score[q];  //s数组按p号分数递减排序
}
char course[4]={'A','C','M','E'};  //按每科优先级顺序,方便输出

int main(int argc, char *argv[]){
    int x,y;int p;
    scanf("%d%d",&x,&y);     //x为要排序的人数,y为要查询的人数
    for(int i=0;i<x;i++){
        scanf("%d%d%d%d",&s[i].id,&s[i].score[1],&s[i].score[2],&s[i].score[3]);   //输入3科分数,并求出平均分(这里直接用总分代替)
        s[i].score[0]=s[i].score[1]+s[i].score[2]+s[i].score[3];
    }
    for(q=0;q<4;q++){            //3科加平均分都排名一次
        sort(s,s+x,cmp);
        Rank[s[0].id][q]=1;      //将排名后的第一个元素名次设为1(直接赋值给id所在位置)
        for(int j=1;j<x;j++){
            if(s[j].score[q]==s[j-1].score[q])       //如果跟前一个元素一样的分数,则有一样的排名
                Rank[s[j].id][q]=Rank[s[j-1].id][q];
            else Rank[s[j].id][q]=j+1;               //跟前一个元素不一样的分数,则排名为下标加1
        }
    }
    for(int i=0;i<y;i++){
        scanf("%d",&p);  //输入查询的id
        if(Rank[p][0]==0) printf("N/A\n");   //该id不存在
        else{
            int num=0;
            for(int k=0;k<4;k++){            //求出该名学生4科中排名最前的一科
                if(Rank[p][k]<Rank[p][num])
                    num=k;
            }
            printf("%d %c\n",Rank[p][num],course[num]);   
        }
    }
    return 0;
}

编辑于 2018-01-31 16:42:04 回复(4)
对于PAT和nowcoder测试时都快自闭了,PAT官网很容易通过,而牛客需要注意几点
发表于 2018-07-29 15:58:07 回复(0)
pat全部通过,牛客这里总是无法通过,无解~~
我用的是笨方法,把问题化成很多简单的小问题然后求解,一开始没有通过,遇到了以下问题,后来改了一下:
1、一开始用数组,后来用了指针,最后一个测试用例过了!不知道为什么;
2、分数相同的学生排名应该相同,改了一下, 一下子通过了pat剩下的3个用例,都是考察这个点的


#include<stdio.h>

#include<string>

#include<string.h>

#include<iostream>

#include<algorithm>

using namespace std;


struct stu2

{

    char id[10];

    double c_score,m_score,e_score,a_score;

    int c_rank,m_rank,e_rank,a_rank;

};


int cmpC(stu2 a,stu2 b)

{

    return a.c_score>b.c_score;

}


int cmpA(stu2 a,stu2 b)

{

    return a.a_score>b.a_score;

}


int cmpM(stu2 a,stu2 b)

{

    return a.m_score>b.m_score;

}


int cmpE(stu2 a,stu2 b)

{

    return a.e_score>b.e_score;

}


void sortStu(stu2 *input,int N)

{

    //a

    sort(input,input+N,cmpA);

    for(int i =0;i<N;i++)

    {

        if(i>0&&input[i].a_score==input[i-1].a_score)

            input[i].a_rank=input[i-1].a_rank;

        else

            input[i].a_rank=i+1;

    }

    //c

    sort(input,input+N,cmpC);

    for(int i =0;i<N;i++)

    {

        if(i>0&&input[i].c_score==input[i-1].c_score)

            input[i].c_rank=input[i-1].c_rank;

        else

            input[i].c_rank=i+1;

    }

    //m

    sort(input,input+N,cmpM);

    for(int i =0;i<N;i++)

    {

        if(i>0&&input[i].m_score==input[i-1].m_score)

            input[i].m_rank=input[i-1].m_rank;

        else

            input[i].m_rank=i+1;

    }

    //e

    sort(input,input+N,cmpE);

    for(int i =0;i<N;i++)

    {

        if(i>0&&input[i].e_score==input[i-1].e_score)

            input[i].e_rank=input[i-1].e_rank;

        else

            input[i].e_rank=i+1;

    }

    

    /*for(int i =0;i<N;i++)

    {

        cout<<input[i].id<<" - "<<input[i].c_rank<<endl;

    }*/

};


void getBestRank(stu2 stu)

{

    if(stu.a_rank<=stu.c_rank&&stu.a_rank<=stu.m_rank&&stu.a_rank<=stu.e_rank)

    {

        //a

        cout<<stu.a_rank<<" A"<<endl;

    }else if(stu.c_rank<=stu.a_rank&&stu.c_rank<=stu.m_rank&&stu.c_rank<=stu.e_rank)

    {

        //c

        cout<<stu.c_rank<<" C"<<endl;

    }else if(stu.m_rank<=stu.a_rank&&stu.m_rank<=stu.c_rank&&stu.m_rank<=stu.e_rank)

    {

        //m

        cout<<stu.m_rank<<" M"<<endl;

    }else 

    {

        //e

        cout<<stu.e_rank<<" E"<<endl;

    }

};


void calBestRank(stu2 *input,char toFind[10],int N)

{

    for(int i=0;i<N;i++)

    {

        if(strcmp(input[i].id,toFind)==0)

        {

            //输出最好的排名A>C>M>E


            getBestRank(input[i]);

            return;

        }

    }

    cout<<"N/A"<<endl;

};

int main()

{

    int N,M;

    scanf("%d %d",&N,&M);

    stu2 *input = new stu2[N];

    for(int i=0;i<N;i++)

    {

        stu2 tmp;

        scanf("%s %lf %lf %lf",tmp.id,&tmp.c_score,&tmp.m_score,&tmp.e_score);

        tmp.a_score = (tmp.c_score+tmp.m_score+tmp.e_score)/3;

        input[i] = tmp;

        //cout<<tmp.id;

    }

    //sort

    sortStu(input,N);


    for(int i=0;i<M;i++)

    {

        char tmp[10];

        scanf("%s",tmp);

        calBestRank(input,tmp,N);

    }

    return 0;

}


发表于 2018-02-17 08:36:47 回复(0)
有点像奥运排序那个题😂
发表于 2022-11-08 19:14:58 回复(0)
#include <iostream>
#include <map>
#include <vector>
#include <string>

using namespace std;

map<string, map<int, vector<char>>> database;
map<int, vector<string>, greater<int>> math;
map<int, vector<string>, greater<int>> cprogramming;
map<int, vector<string>, greater<int>> english;
map<int, vector<string>, greater<int>> average;
int main(){
    int n;
    int m;
    cin >> n;
    cin >> m;
    string id;
    int mathGrade;
    int cprogrammingGrade;
    int englishGrade;
    for(int i = 0; i < n; i++){
        cin >> id;
        cin >> cprogrammingGrade;
        cin >> mathGrade;
        cin >> englishGrade;

        cprogramming[cprogrammingGrade].push_back(id);
        math[mathGrade].push_back(id);
        english[englishGrade].push_back(id);
        int averageGrade = (mathGrade + cprogrammingGrade + englishGrade) / 3;
        average[averageGrade].push_back(id);
    }
    int count = 1;
    for(map<int, vector<string>>::iterator it = average.begin(); it != average.end(); ++it){
        int tieCount = 0;
        for(vector<string>::iterator itr = it->second.begin(); itr != it->second.end(); ++itr){
            database[*itr][count].push_back('A');
            //cout << "push A=" << it->first << "at" << distance(average.begin(), it) + 1 << endl;
            tieCount++;
        }
        count += tieCount;
    }
    count = 1;
    for(map<int, vector<string>>::iterator it = cprogramming.begin(); it != cprogramming.end(); ++it){
        int tieCount = 0;
        for(vector<string>::iterator itr = it->second.begin(); itr != it->second.end(); ++itr){
            database[*itr][count].push_back('C');
            tieCount++;
        }
        count += tieCount;
    }
    count = 1;
    for(map<int, vector<string>>::iterator it = math.begin(); it != math.end(); ++it){
        int tieCount = 0;
        for(vector<string>::iterator itr = it->second.begin(); itr != it->second.end(); ++itr){
            database[*itr][count].push_back('M');
            tieCount++;
        }
        count += tieCount;
    }
    count = 1;
    for(map<int, vector<string>>::iterator it = english.begin(); it != english.end(); ++it){
        int tieCount = 0;
        for(vector<string>::iterator itr = it->second.begin(); itr != it->second.end(); ++itr){
            database[*itr][count].push_back('E');
            tieCount++;
        }
        count += tieCount;
    }
    for(int i = 0; i < m; i++){
        cin >> id;
        if(database.count(id)){
            cout << database[id].begin()->first << " " << database[id].begin()->second[0] << endl;
        }else{
            cout << "N/A" << endl;
        }
    }
    return 0;
}
发表于 2021-01-17 23:22:32 回复(0)
/*
ID有字符,得用字符串存;
平均分向下取整;
形同分数名次相同;
*/

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;

struct xx{
int j1,j2,j3,z,lev,te;
}t;

vector<xx> man;
vector<int> xu;
vector<int> cha;
map<string,int> re;
map<string,int>::iterator it;

bool pai1(int a,int b){
return man[a].z>man[b].z;
}

bool pai2(int a,int b){
return man[a].j1>man[b].j1;
}

bool pai3(int a,int b){
return man[a].j2>man[b].j2;
}

bool pai4(int a,int b){
return man[a].j3>man[b].j3;
}


int main(){                 //main
int i,j,k,n,m;
char shi[4]={'C','M','E','A'};
string pp;
scanf("%d %d",&n,&m);
i=0;
while(i<n){
    cin>>pp;
scanf("%d %d %d",&t.j1,&t.j2,&t.j3);
re[pp]=i;                                  //map,字符串和数组下标
t.z=(t.j1+t.j2+t.j3)/3;                    //平均分
man.push_back(t);
xu.push_back(i);
i++;}
                                                            //   按 A > C > M > E 顺序排序
sort(xu.begin(),xu.end(),pai1);
i=0;
while(i<n){
if(i==0||man[xu[i]].z<man[xu[i-1]].z)
j=i+1;                                                    //相同分数排名不变
man[xu[i]].te=j;
man[xu[i]].lev=3;
i++;}

sort(xu.begin(),xu.end(),pai2);
i=0;j=0;
while(i<n){
if(i==0||man[xu[i]].j1<man[xu[i-1]].j1)
j=i+1;

if(j<man[xu[i]].te){
man[xu[i]].te=j;
man[xu[i]].lev=0;}                   //当前名次更小,更新名次及考试名称
i++;}

sort(xu.begin(),xu.end(),pai3);
i=0;j=0;
while(i<n){
if(i==0||man[xu[i]].j2<man[xu[i-1]].j2)
j=i+1;


if(j<man[xu[i]].te){
man[xu[i]].te=j;
man[xu[i]].lev=1;}
i++;}

sort(xu.begin(),xu.end(),pai4);
i=0;j=0;
while(i<n){
if(i==0||man[xu[i]].j3<man[xu[i-1]].j3)
j=i+1;


if(j<man[xu[i]].te){
man[xu[i]].te=j;
man[xu[i]].lev=2;}
i++;}

i=0;
while(i<m){
cin>>pp;
it=re.find(pp);
if(it==re.end())
    j=-1;
else
j=it->second;
cha.push_back(j);
i++;
}

i=0;
while(i<m){
    j=cha[i];
if(j==-1)
printf("N/A\n");
else
printf("%d %c\n",man[j].te,shi[man[j].lev]);

i++;}

return 0;}
发表于 2020-07-14 22:49:46 回复(0)
1.对于名次:    若有分数表 100 98 97 97 97 96 95
排名情况为                            1    2   3   3   3   6   8
即相同分数排名相同,不同就取当前实际名次。
2.对于总分处理:
牛客(能过):总分除以3,再向下取整。
我的想法(不能过,但pat能过):我觉得不必求平均分,直接取总分更好,否则总分为290    289    288的三个同学没有了区分度。
3.感想:牛客真**

最后附上pat能过的代码(查找用了二分):
#include<cstdio>
#include<iostream>
#include<vector>
#include<unordered_map>
#include<string>
#include<algorithm>

using namespace std;

#define MAX 2010

struct Student
{
	int grade[4];
	int rank[4];
};

int N, M;
Student stu[MAX];
string outname[MAX];
unordered_map<string, Student> mp;

bool cmp(int a, int b)
{
	return a > b;
}

int getRank(int grade, vector<int>& list)	//二分
{
	int a = 0, b = N - 1, t = 0;

	while (grade != list[t])
	{
		t = (a + b) / 2;
		if (grade < list[t])
			a = t + 1;
		else if (grade > list[t])
			b = t - 1;
	}

	while (t > 0 && list[t - 1] == list[t])
	{
		t--;
	}

	return t + 1;	//返回的是下标,要转成排名
}

int main()
{
	scanf("%d %d", &N, &M);

	int i, j, t, sum;
	string name;
	vector<int> ranklist[4];	//维护四个科目总排名

	for (i = 0; i < N; i++)
	{
		cin >> name;
		sum = 0;
		for (j = 1; j < 4; j++)
		{
			scanf("%d", &t);
			sum += t;
			stu[i].grade[j] = t;
			ranklist[j].push_back(t);	//建立成绩表
		}
		stu[i].grade[0] = sum;
		ranklist[0].push_back(sum);
		mp[name] = stu[i];
	}

	for (i = 0; i < 4; i++)
		sort(ranklist[i].begin(), ranklist[i].end(), cmp);

	for (i = 0; i < M; i++)
		cin >> outname[i];

	int gbest;
	char cbest;
	for (i = 0; i < M; i++)
	{
		if (mp.find(outname[i]) != mp.end())
		{
			for (j = 0; j < 4; j++)
			{
				mp[outname[i]].rank[j] = getRank(mp[outname[i]].grade[j], ranklist[j]);

				if (j == 0)
				{
					gbest = mp[outname[i]].rank[j];
					cbest = 'A';
				}
				else if (gbest > mp[outname[i]].rank[j])	//排名越小越好
				{
					gbest = mp[outname[i]].rank[j];
					if (j == 1)
						cbest = 'C';
					else if (j == 2)
						cbest = 'M';
					else if (j == 3)
						cbest = 'E';
				}
			}
			printf("%d %c\n", gbest, cbest);
		}
		else
		{		//没找到
			printf("N/A\n");
		}
	}

	return 0;
}




发表于 2020-07-13 12:23:22 回复(0)
#include<bits/stdc++.h>
using namespace std;
int tag;
int C = 0,M =1,E = 2, A= 3, CR =4, MR=5, ER=6, AR=7;
bool cmp(pair<string,vector<int>>& a, pair<string,vector<int>>& b) {
    return (a.second[tag] > b.second[tag]);
}

int main() {
    int m, n;
    cin>>n>>m;
    vector<pair<string,vector<int>>> db;
    unordered_map<string, char> hashmap;
    
    while (n--) {
        pair<string, vector<int>> mp;
        string id;
        int a,b,c,d;
        cin>>id>>a>>b>>c;
        mp.first = id;
        d = (a+b+c) / 3;
        mp.second = {a,b,c,d,0,0,0,0};
        db.emplace_back(mp);
        hashmap[id]++;
    }
    for (tag = 0; tag < 4; tag++) {
        sort(db.begin(), db.end(), cmp);
        for (int i = 0; i < db.size(); i++) {
            if (i > 0 && db[i].second[tag] == db[i - 1].second[tag]){
                db[i].second[tag + 4] = db[i - 1].second[tag + 4];
            }else 
                db[i].second[tag + 4] = i+1;
        }
    }

    while (m--) {
        string id;
        cin>>id;
        if (hashmap.find(id) == hashmap.end()) {
            cout<<"N/A"<<endl;
        } else {
            for (auto& t:db) {
                if (t.first == id) {
                    if (t.second[AR] <= t.second[MR] && t.second[AR] <= t.second[ER] && t.second[AR] <= t.second[CR]) {
                        cout<<t.second[AR]<<" "<<"A"<<endl;
                    } else if (t.second[CR] <= t.second[MR] && t.second[CR] <= t.second[ER] && t.second[CR] <= t.second[AR]) {
                        cout<<t.second[CR]<<" "<<"C"<<endl;
                    } else if (t.second[MR] <= t.second[AR] && t.second[MR] <= t.second[ER] && t.second[MR] <= t.second[CR]) {
                        cout<<t.second[MR]<<" "<<"M"<<endl;
                    } else if(t.second[ER] <= t.second[MR] && t.second[ER] <= t.second[AR] && t.second[ER] <= t.second[CR]) {
                        cout<<t.second[ER]<<" "<<"E"<<endl;
                    } 
                    break;
                }
            }
        }
    }
    return 0;







用map 存放 id  c m e a cr mr er ar ,分别为 : {学号,C语言成绩,数学成绩,英语成绩,平均分,C语言排名, 数学排名,英语排名,平均分 排名};
1.获取所有同学的信息,放入 db 中
2.将  c m e a 分别排序 ,并将每个同学的排名 存入 cr mr er ar 中
3.对于 进行查询的同学, 比较 cr mr er ar 输出数值较小的
发表于 2020-02-21 20:54:08 回复(0)
牛客全不过,匪夷所思;
还有难道求平均成绩的那个不是直接求和处理就行了吗,不用费力求真正平均值吧;
a = list(map(int,input().split()))
d,m,n,o,p,q ={},[],[],[],[],['A','C','M','E']
for i in range(a[0]):
    b = input().split()
    d[b[0]] = i
    m.append(sum(map(int,b[1:])))
    for j in range(1,4):
        eval(chr(109 + j)).append(int(b[j]))

q = ['A','C','M','E']
for i in range(a[1]):
    b = input()
    if b in d:
        s = a[0]
        for j in range(4):
            r = len(list(filter(lambda x:x > eval(chr(109 + j))[d[b]],eval(chr(109 + j))))) + 1
            if r < s:
                s,t = r,q[j]
                
        print(s,t)
    else:
        print('N/A')
编辑于 2020-02-18 21:07:54 回复(0)
求助...排名规则到底是咋样的,按道理来说相同分数的排名应该相同来着,但是对于下面这个测试数据:
8 8
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
999999 85 90 90
999998 85 90 91
999997 0 0 0
310101
310102
310103
310104
310105
999999
999998
999997

错误答案是:
1 C
1 M
1 E
1 A
3 A
3 A
2 E
5 E

这题题解的答案是:
1 C
1 M
1 E
1 A
3 A
3 A
2 E
8 A

按照两个3A的输出来看,应该是分数相同的排名相同啊,但是对于最后一个测试数据,明显是用E进行排名是排名更高,因为E分数里有很多人有相同分数..
发表于 2019-08-16 16:45:51 回复(0)
思路很简单,代码很繁琐…代码繁琐的主要原因还是我太菜…
1.将每个科目的成绩单独放在一个数组(c[ ] m[ ] e[ ] a[ ]),每个数组下标和存放id的数组下标对应。
2.对每个科目的成绩排名(cp[ ] mp[ ]  ep[ ] ap[ ])
3.匹配学号j,找出cp[j] mp[j] ep[j] ap[j]中最小的数
嗯…我得再学习一下大佬的代码…
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int main(){
    string s[2005],h[2005];
    int c[2005],m[2005],e[2005],a[2005];
    int cp[2005],mp[2005],ep[2005],ap[2005];
    memset(cp,1,sizeof(cp));
    memset(mp,1,sizeof(mp));
    memset(ep,1,sizeof(ep));
    memset(ap,1,sizeof(ap));
    char nn[4]={'A','C','M','E'};
    int n,k,i,j,d;
    int maxx=0;
    int t=0;
    cin >> n >> k;
    for(i=0;i<n;i++){
        cin >> s[i] >> c[i] >> m[i] >> e[i];
    }
    for(j=0;j<k;j++){
        cin >> h[j];
    }
    for(i=0;i<n;i++){
        a[i]=(c[i]+m[i]+e[i])/3;
    }
    if(n==1){
        cout << "1" <<" " << "A" <<endl;
    }
    else{
    for(i=0;i<n;i++){
        t=1;
        for(j=0;j<n;j++){
            if(c[j]>c[i]){
                t++;
            }
        }
        cp[i]=t;
    }
    for(i=0;i<n;i++){
        t=1;
        for(j=0;j<n;j++){
            if(a[j]>a[i]){
                t++;
            }
        }
        ap[i]=t;
    }
    for(i=0;i<n;i++){
        t=1;
        for(j=0;j<n;j++){
            if(m[j]>m[i]){
                t++;
            }
        }
        mp[i]=t;
    }
    for(i=0;i<n;i++){
        t=1;
        for(j=0;j<n;j++){
            if(e[j]>e[i]){
                t++;
            }
        }
        ep[i]=t;
    }
    //cout << cp[0] <<" "<< ap[0] <<" "<< ep[0] <<" "<< mp[0];
    int res[5];
    int temp;

    for(i=0;i<k;i++){
        int r=0;
        for(j=0;j<n;j++){
            if(h[i]==s[j]){
                r++;
                res[0]=ap[j];
                res[1]=cp[j];
                res[2]=mp[j];
                res[3]=ep[j];
                int minn=n;
                for(d=0;d<4;d++){
                    if(minn>res[d]){
                        minn=res[d];
                        temp=d;
                    }
                }
                cout << minn << " " << nn[temp]<<endl;
            }
        }
        if(r==0){
            cout << "N/A" <<endl;
        }
    }}
}


发表于 2019-06-10 17:41:03 回复(0)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstring>
#include<string.h>

using namespace std;

struct Stu{
    char id[10];
    int grades[4];
    int R[4];
    char Best;
    int min;
}stu[2010];

int now;                //注意:定义在cmp前面 

bool cmp(Stu a,Stu b){
    return a.grades[now] > b.grades[now];
}

int main(){
    int N,M,i,j,Min;
    //map<int,Stu> id_best;
    char subject[4] = {'A','C','M','E'};
    scanf("%d%d",&N,&M);
    for(i=0; i<N; i++){
        scanf("%s%d%d%d",&stu[i].id, &stu[i].grades[1], &stu[i].grades[2], &stu[i].grades[3]);
        stu[i].grades[0] = round((stu[i].grades[1] + stu[i].grades[2] + stu[i].grades[3]) / 3.0); 
        //cout<<stu[i].grades[0]<<endl;
    }
    
    for(now=0; now<4; now++)                                         //对四个成绩进行排名 
    {
        sort(stu,stu+N,cmp);                                    //+1:因为从1开始存放    
        stu[0].R[now] = 1;
        for(j=1; j<N; j++){                                        //相同分数应有相同排名 
            if(stu[j].grades[now] == stu[j-1].grades[now])
                stu[j].R[now] = stu[j-1].R[now];
            else  
                stu[j].R[now] = j+1;
        }                
    }
    
    //cout<<stu[0].R[0];
     
    for(i=0; i<N; i++){
        Min = *(min_element(stu[i].R,stu[i].R+4));
        stu[i].min = Min;
        bool flag1 = false;
        for(j=0; j<4; j++){
            if(Min==stu[i].R[j] && flag1 == false) {
                stu[i].Best = subject[j];
                flag1 = true;
            }
        }
        //cout<<Min<<" ";    
    }
    
    
    char Id[10];
    for(i=0; i<M; i++){
        scanf("%s",&Id);
        bool flag = false;
        for(j=0; j<N;j++){
            if(!strcmp(Id,stu[j].id)){
                
                printf("%d %c\n",stu[j].min,stu[j].Best);
                flag = true;
            
        }
        if(flag == false) cout<<"N/A"<<endl;
    }
    
    return 0;
}

发表于 2019-05-22 20:45:55 回复(0)
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

//1012 The Best Rank
//三节课 C,M,E,平均分A
//排4次名,取最高
//A>C>M>E
//输入先n行数据,再m行查询
//不在列表中取N/A

#define maxn 2001
int C[maxn] = {}, M[maxn] = {};
int E[maxn] = {}, A[maxn] = {};

char item[4] = {
    'A','C','M','E'
};

struct student {
    string id;
    int no_; //题目中给的6位编号没用,不如存下标
    int c, m, e, a;
    bool  operator == (const string &s)const 
    {
        return id == s;
    }

};


int getRank(int score, int *z) {
    int n = 0;
    while (score<z[n]) {
        n++;
    }
    return n + 1;
}


pair<int, char> getBestRank(int i, const student *s) {
    int rank[4] = {}; //A,C,M,E的顺序
    int b = 0;
    rank[0] = getRank(s[i].a, A);
    rank[1] = getRank(s[i].c, C);
    rank[2] = getRank(s[i].m, M);
    rank[3] = getRank(s[i].e, E);

    for (int j = 0; j < 4; ++j) {
        if (rank[j] < rank[b]) b = j;
    }

    return { rank[b],item[b] };
}

bool compare(const int x, const int y) {
    return x > y;
}

int main() {
    int n, m;//<=2000
    pair<int, char> bestRank[maxn] = {};
    student stu[maxn];


    cin >> n >> m;

    string id;
    int x, y, z;
    for (int i = 0; i < n; ++i) {
        cin >> id >> x >> y >> z;
        int avg = floor((x + y + z) / 3);
        C[i] = x; M[i] = y;
        E[i] = z; A[i] = avg;
        stu[i] = { id, i,x,y,z, avg };
    }

    //降序
    sort(A, A + n, compare);
    sort(C, C + n, compare);
    sort(M, M + n, compare); sort(E, E + n, compare);

    //排名
    for (int i = 0; i < n; ++i) {
        bestRank[i] = getBestRank(i, stu);
    }

    for (int i = 0; i < m; ++i) {
        cin >> id;
        student* ptr = find(stu, stu + n, id); //返回所在位置的指针
        if (ptr == stu + n) { //查找失败
            cout << "N/A" << endl;
        }
        else {
            x = ptr->no_;//获得下标
            cout << bestRank[x].first << " " << bestRank[x].second << endl;
        }

    }

    return 0;
}

发表于 2019-01-14 11:07:16 回复(0)
g1,g2,g3,g4 = [],[],[],[]
stu = {}
n,m = map(int,input().split())
for i in range(n):
    t = input().split()
#    stu[t[0]]=[(int(t[1])+int(t[2])+int(t[3]))//3,int(t[1]),int(t[2]),int(t[3])]
    g1.append([t[0],(int(t[1])+int(t[2])+int(t[3]))//3])
    g2.append([t[0],int(t[1])])
    g3.append([t[0],int(t[2])])
    g4.append([t[0],int(t[3])])
g1.sort(key = lambda x:(-x[1]))
g2.sort(key = lambda x:(-x[1]))
g3.sort(key = lambda x:(-x[1]))
g4.sort(key = lambda x:(-x[1]))

con = 1
stu[g1[0][0]],temp = [con,'A'],g1[0][1]
for i in range(1,n):
    if g1[i][1]==temp:
        stu[g1[i][0]] = [con,'A']
    else:
        con = i+1
        temp = g1[i][1]
        stu[g1[i][0]] = [con,'A']

con = 1
temp = g2[0][1]
if stu[g2[0][0]][0]>con:
   stu[g2[0][0]] = [con,'C'] 
for i in range(1,n):
    if g2[i][1]==temp:
        if stu[g2[i][0]][0]>con:
            stu[g2[i][0]] = [con,'C']
    else:
        con = i+1
        temp = g2[i][1]
        if stu[g2[i][0]][0]>con:
            stu[g2[i][0]] = [con,'C']

con = 1
temp = g3[0][1]
if stu[g3[0][0]][0]>con:
   stu[g3[0][0]] = [con,'M'] 
for i in range(1,n):
    if g3[i][1]==temp:
        if stu[g3[i][0]][0]>con:
            stu[g3[i][0]] = [con,'M']
    else:
        con = i+1
        temp = g3[i][1]
        if stu[g3[i][0]][0]>con:
            stu[g3[i][0]] = [con,'M']
con = 1
temp = g4[0][1]
if stu[g4[0][0]][0]>con:
   stu[g4[0][0]] = [con,'E'] 
for i in range(1,n):
    if g4[i][1]==temp:
        if stu[g4[i][0]][0]>con:
            stu[g4[i][0]] = [con,'E']
    else:
        con = i+1
        temp = g4[i][1]
        if stu[g4[i][0]][0]>con:
            stu[g4[i][0]] = [con,'E']
for j in range(m):
    query = input()
    if query not in stu:
        print("N/A")
    else:
        print(str(stu[query][0])+" "+stu[query][1])
这道题本来是一道水题,但是由于排名的规则没有说清楚,调代码也是调的让人头大。而且,个人觉得该题目也要说清楚平均分得出的是取整还是四舍五入还是可保留小数位的。如果能说清楚的话会省去不少调试带来的烦恼。
编辑于 2018-12-20 14:45:58 回复(0)
例子里面A用的四舍五入,测试用例用的向下取整,也是有点迷。
310103     82 87 94 88

发表于 2018-10-03 21:57:14 回复(0)
思路:使用类的方法处理
注意点,根据上面大佬所讲,首先 平均数,是整数,注意。然后没有然后了。
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;

#ifndef debug
ifstream ifile("case.txt");
#define cin ifile
#endif

int priorities[4] = { 1,2,3,4 };
const string strRankNam[] = { "A","C","M","E" };
class student
{
public:
    string name;
    int C;
    int M;
    int E;
    int A;
    int rank[4] = {0};// A C M E
    //string bestRank;
public:
    void BestRank()
    {
        int minRank = 0;
        for (int i = 1; i < 4; i++)
        {
            if (rank[minRank] > rank[i])
            {
                minRank = i;
            }
            else if (rank[minRank] == rank[i] && rank[minRank] * priorities[minRank] > rank[i] * priorities[i])
            {
                minRank = i;
            }
        }
        cout << rank[minRank] << " " << strRankNam[minRank] << endl;
    }
    void Avg()
    {
        this->A = (this->C + this->E + this->M) / 3;
    }
};

bool Cmp1(student &a, student &b)// 平均分排名
{
    return a.A > b.A;
}
bool Cmp2(student &a, student &b)// 平均分排名
{
    return a.C > b.C;
}
bool Cmp3(student &a, student &b)// 平均分排名
{
    return a.M > b.M;
}
bool Cmp4(student &a, student &b)// 平均分排名
{
    return a.E > b.E;
}



int main()
{
    int N, Mm;
    cin >> N >> Mm;
    map<string, int> mp;
    vector<student> v(N);
    string id;
    int C, M, E;
    double avg;
    for (int i = 0; i < N; i++)
    {
        cin >> v[i].name;
        cin >> v[i].C >> v[i].M >> v[i].E;
        v[i].Avg();
    }
    sort(v.begin(), v.end(), Cmp1);
    v[0].rank[0] = 1;
    for (int i = 1; i < N; i++)
    {
        if (v[i].A == v[i - 1].A)
        {
            v[i].rank[0] = v[i - 1].rank[0];
        }
        else
        {
            v[i].rank[0] = i + 1;
        }
    }
    sort(v.begin(), v.end(), Cmp2);
    v[0].rank[1] = 1;
    for (int i = 1; i < N; i++)
    {
        if (v[i].C == v[i - 1].C)
        {
            v[i].rank[1] = v[i - 1].rank[1];
        }
        else
        {
            v[i].rank[1] = i + 1;
        }
    }
    sort(v.begin(), v.end(), Cmp3);
    v[0].rank[2] = 1;
    for (int i = 1; i < N; i++)
    {
        if (v[i].M == v[i - 1].M)
        {
            v[i].rank[2] = v[i - 1].rank[2];
        }
        else
        {
            v[i].rank[2] = i + 1;
        }
    }
    sort(v.begin(), v.end(), Cmp4);
    v[0].rank[3] = 1;
    for (int i = 1; i < N; i++)
    {
        if (v[i].E == v[i - 1].E)
        {
            v[i].rank[3] = v[i - 1].rank[3];
        }
        else
        {
            v[i].rank[3] = i + 1;
        }
    }
    for (int i = 0; i < N; i++)
    {
        mp[v[i].name] = i;
        if (i == 0)
            mp[v[i].name] = -1;
    }
    for (int i = 0; i < Mm; i++)
    {
        cin >> id;
        if (mp[id] != 0)
        {
            if (mp[id] == -1)
                v[0].BestRank();
            else
                v[mp[id]].BestRank();
        }
        else
            cout << "N/A" << endl;
    }
    system("pause");
}

发表于 2018-08-30 19:21:17 回复(0)

老老实实的笨办法

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

const char symbol[4] = { 'A', 'C', 'M', 'E' };

struct stu
{
    string ID;
    int A, C, M, E;
    vector<int> rank; 
    stu() {
        this->rank.resize(4);
    }
    bool operator == (const string &i) { 
        return this->ID == i;
    }
};

vector<stu> stus;
vector<string> query_ids;

bool Comp1(const stu &a, const stu &b) 
{
    return a.A > b.A;
}

bool Comp2(const stu &a, const stu &b)
{
    return a.C > b.C;
}

bool Comp3(const stu &a, const stu &b)
{
    return a.M > b.M;
}

bool Comp4(const stu &a, const stu &b)
{
    return a.E > b.E;
}

int main()
{
    int M, N;
    cin >> M >> N;
    for (int i = 0; i < M; i++) {
        stu temp;
        cin >> temp.ID;
        cin >> temp.C;
        cin >> temp.M;
        cin >> temp.E;
        temp.A = (temp.C + temp.M + temp.E) / 3;
        stus.push_back(temp);
    }
    query_ids.resize(N);
    for (int i = 0; i < N; i++)
        cin >> query_ids[i];

    sort(stus.begin(), stus.end(), Comp1);
    int count = 1;
    stus[0].rank[0] = 1;
    for (int i = 1; i < stus.size(); i++) {
        if (stus[i].A == stus[i - 1].A) {
            stus[i].rank[0] = stus[i - 1].rank[0];
            count++;
        }
        else {
            stus[i].rank[0] = stus[i - 1].rank[0] + count;
            count = 1;
        }
    }
    sort(stus.begin(), stus.end(), Comp2);
    count = 1;
    stus[0].rank[1] = 1;
    for (int i = 1; i < stus.size(); i++) {
        if (stus[i].C == stus[i - 1].C) {
            stus[i].rank[1] = stus[i - 1].rank[1];
            count++;
        }
        else {
            stus[i].rank[1] = stus[i - 1].rank[1] + count;
            count = 1;
        }
    }
    sort(stus.begin(), stus.end(), Comp3);
    count = 1;
    stus[0].rank[2] = 1;
    for (int i = 1; i < stus.size(); i++) {
        if (stus[i].M == stus[i - 1].M) {
            stus[i].rank[2] = stus[i - 1].rank[2];
            count++;
        }
        else {
            stus[i].rank[2] = stus[i - 1].rank[2] + count;
            count = 1;
        }
    }
    sort(stus.begin(), stus.end(), Comp4);
    count = 1;
    stus[0].rank[3] = 1;
    for (int i = 1; i < stus.size(); i++) {
        if (stus[i].E == stus[i - 1].E) {
            stus[i].rank[3] = stus[i - 1].rank[3];
            count++;
        }
        else {
            stus[i].rank[3] = stus[i - 1].rank[3] + count;
            count = 1;
        }
    }

    vector<stu>::iterator iter;
    int min_pos;
    for (int i = 0; i < query_ids.size(); i++) {
        if ((iter=find(stus.begin(), stus.end(), query_ids[i])) == stus.end()) {
            cout << "N/A" << endl;
        }
        else {
            min_pos = min_element(stus[iter - stus.begin()].rank.begin(), 
                stus[iter - stus.begin()].rank.end()) - stus[iter - stus.begin()].rank.begin();
            cout << stus[iter - stus.begin()].rank[min_pos] << " " << symbol[min_pos] << endl;
        }
    }

    return 0;
}
发表于 2018-02-09 14:55:14 回复(0)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class TheBestRank {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);

    int N=reader.nextInt();
    int M=reader.nextInt();

    HashMap info=new HashMap();
    ArrayList agrade=new ArrayList();
    ArrayList cgrade=new ArrayList();
    ArrayList mgrade=new ArrayList();
    ArrayList egrade=new ArrayList();

    int[] score;

    String id;
    int c,m,e,a;
    for(int i=0;i<N;i++){
        score=new int[4];

        id=reader.next();
        c=reader.nextInt();
        m=reader.nextInt();
        e=reader.nextInt();

        a=(c+m+e)/3;

        agrade.add(a);
        cgrade.add(c);
        mgrade.add(m);
        egrade.add(e);

        score[0]=a;
        score[1]=c;
        score[2]=m;
        score[3]=e;

        info.put(id, score);
    }

    Collections.sort(agrade);
    Collections.sort(cgrade);
    Collections.sort(mgrade);
    Collections.sort(egrade);

    a=0;
    c=0;
    m=0;
    e=0;
    for(int i=0;i<M;i++){
        id=reader.next();
        if(!info.containsKey(id)){
            System.out.println("N/A");
        }else{
            score=info.get(id);

            for(int j=0;j<N;j++){
                if(score[0]>=agrade.get(j)){
                    a=j;
                }

                if(score[1]>=cgrade.get(j)){
                    c=j;
                }

                if(score[2]>=mgrade.get(j)){
                    m=j;
                }

                if(score[3]>=egrade.get(j)){
                    e=j;
                }
            }
            if(a>=c&&a>=m&&a>=e){
                System.out.println(N-a+" A");
            }else if(c>=m&&c>=e){
                System.out.println(N-c+" C");
            }else if(m>=e){
                System.out.println(N-m+" M");
            }else {
                System.out.println(N-e+" E");
            }
        }
    }
    reader.close();
}

}

发表于 2018-01-20 14:03:15 回复(0)