A1012 The Best Rank (25 分)

需要注意的点:成绩排名为并列时的代码。

#include <iostream>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct Student{
    
    
	int id;
	int grade[4]; //存放四个分数 
}stu[2010];

int now = 0;
int rankId[100000000][4] = {
    
    0};
char course[4] = {
    
    'A', 'C', 'M', 'E'};

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

int main(int argc, char** argv) {
    
    
	
	int stuId[2010];
	int n, m;
	cin >> n >> m;
	
	//输入数据  学号+成绩 
	for(int i = 0; i < n; i++){
    
    
		cin >> stu[i].id >> 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]); 
	}
	//输入待查的学号 
	for(int i = 0; i < m; i++){
    
    
		cin >> stuId[i];
	}
	
	//算出每个人每门成绩的排名 
	for(now = 0; now < 4; now++){
    
    
		sort(stu, stu + n, cmp); 
		rankId[stu[0].id][now] = 1; //该同学该课为第一名
		//其他同学排序;//考虑并列的情况 
		for(int i = 1; i < n; i++){
    
    
			if(stu[i].grade[now] == stu[i-1].grade[now]){
    
    //如果和上一名同学成绩一样,则为并列 
				rankId[stu[i].id][now] =  rankId[stu[i-1].id][now];
			} else {
    
    
				rankId[stu[i].id][now] =  i+1;
			}
		} 
	}
	
	//查询 
	for(int i = 0; i < m; i++) {
    
    
		if(rankId[stuId[i]][0] == 0){
    
    
			cout << "N/A" << endl;
		} else {
    
    
			int k = 0;
			for(int j = 0; j < 4; j++){
    
    
				if(rankId[stuId[i]][j] < rankId[stuId[i]][k]){
    
    
					k = j;
				}
			}
			cout << rankId[stuId[i]][k] << " " << course[k] << endl;
		}
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/alovelypeach/article/details/114282338
今日推荐