1012 The Best Rank

题目

在这里插入图片描述

翻译

给他们进行排名,以A>C>M>E的顺序进行排列
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
结尾无空行

输入:N M
N个 ID C M E
M个ID
输出:
M个ID的最好成绩的顺序以及相应的名称

思路

读入N M
通过结构体进行存储数据
按照顺序读入成绩并计算平均分
进行排序sort 构造出cmp函数
!!!!一开始已经声明了flag,后面在for函数里面就不用声明直接用就好
注意:相同成绩的会是相同的排名

代码


```cpp
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

struct Student{
	int id;
	int score[4],rank[4];
	int best_index;
}stu[2000];

int flag;
bool cmp(Student a,Student b){
	return a.score[flag]>b.score[flag];
}
int exist[1000000]={0};
int main()
{	int N,M;
	cin>>N>>M;
 //读入成绩 
	for(int i=0;i<N;i++)
	{
		cin>>stu[i].id>>stu[i].score[1]>>stu[i].score[2]>>stu[i].score[3];
		
		stu[i].score[0]=round((stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3.0);
		//cout<<stu[i].id<<" "<<stu[i].score[0]<<endl;	
	}
	
	for ( flag = 0; flag <= 3; flag++) {
        // 根据这一项成绩排名
        sort(stu,stu+N,cmp);
        // 把根据这一分数的排名结果存进stu.rank[4]对应的那一栏
        for (int i = 0; i < N; i++) {
            // 这一项分数相同就并列,从i = 1开始要判断这个
            if(i==0||stu[i].score[flag]!=stu[i-1].score[flag])
                stu[i].rank[flag]=i+1;
            else // 并列
                stu[i].rank[flag] = stu[i - 1].rank[flag];
            //cout<<stu[i].id<<" "<<stu[i].rank[flag]<<endl; 
        }
    }
	

	//在同一个id当中选出一个最好
	for(int i=0;i<N;i++)
	{
		stu[i].best_index=0;
		for(int j=1;j<4;j++)
		{
			if(stu[i].rank[j]<stu[i].rank[stu[i].best_index])
				stu[i].best_index=j;
				
		 }
		 //cout<<stu[i].best_index<<" ";
		exist[stu[i].id] = i+1;  
	 } 
	 int id;
	 char s[5]={'A','C','M','E'};
	 for(int i=0;i<M;i++) 
	 {
	 	
	 	cin>>id;
	 	
	 		if(exist[id]==0)
	 			cout<<"N/A"<<endl;
	 		else {
	 			int index=exist[id]-1;
	 			cout<<stu[index].rank[stu[index].best_index]<<" "<<s[stu[index].best_index]<<endl;
			 }
		 
	 }	 
	 return 0;
			
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值