pat 1012 The Best Rank

代码极其冗余,有重复时注意排名顺序。 1 2 3 3 5   不是 1 2 3 3 4 。。。。。

AC代码:

#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;
#define NUM 2005
struct Student
{
	char str[9];
	int c;
	int m;
	int e;
	int a;

}s[NUM];

int min(int a, int b)
{
	return a<b?a:b;
}

void find(Student s1)
{
	int x=min(s1.c,s1.e);
	int y=min(s1.m,s1.a);
	int temp=min(x,y);
	if(temp==s1.a)
		printf("%d A\n",temp);
	else if(temp==s1.c)
		printf("%d C\n",temp);
	else if(temp==s1.m)
		printf("%d M\n",temp);
	else if(temp==s1.e)
		printf("%d E\n",temp);
	
}

int cmp_c(const void *a, const void *b)
{
	Student *x=(Student *)a;
	Student *y=(Student *)b;
	return y->c - x->c;
}
int cmp_m(const void *a, const void *b)
{
	Student *x=(Student *)a;
	Student *y=(Student *)b;
	return y->m - x->m;
}
int cmp_e(const void *a, const void *b)
{
	Student *x=(Student *)a;
	Student *y=(Student *)b;
	return y->e - x->e;
}
int cmp_a(const void *a, const void *b)
{
	Student *x=(Student *)a;
	Student *y=(Student *)b;
	return y->a - x->a;
}

int main()
{

	int n,m,i;
	char stuId[9];

	freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);

	cin>>n>>m;
	for(i=0;i<n;i++){
		cin>>s[i].str>>s[i].c>>s[i].m>>s[i].e;
		s[i].a=s[i].c+s[i].m+s[i].e;
	}
	int score,rank;
	score=rank=-1;
	qsort(s,n,sizeof(s[0]),cmp_c);
	for(i=0;i<n;i++){
		if(s[i].c!=score){
			rank=i+1;
			score=s[i].c;
			s[i].c=i+1;			
		} else {
			s[i].c=rank;
		}
		
	}
	score=rank=-1;
	qsort(s,n,sizeof(s[0]),cmp_m);
	for(i=0;i<n;i++){
		if(s[i].m!=score){
			rank=i+1;
			score=s[i].m;
			s[i].m=i+1;			
		} else {
			s[i].m=rank;
		}
	}
	score=rank=-1;
	qsort(s,n,sizeof(s[0]),cmp_e);
	for(i=0;i<n;i++){
		if(s[i].e!=score){
			rank=i+1;
			score=s[i].e;
			s[i].e=i+1;			
		} else {
			s[i].e=rank;
		}
	}
	score=rank=-1;
	qsort(s,n,sizeof(s[0]),cmp_a);
	for(i=0;i<n;i++){
		if(s[i].a!=score){
			rank=i+1;
			score=s[i].a;
			s[i].a=i+1;			
		} else {
			s[i].a=rank;
		}
	}
	int flag;
	while(m--){
		flag=0;
		cin>>stuId;
		for(i=0;i<n;i++){
			if(strcmp(s[i].str,stuId)==0){
				find(s[i]);
				flag=1;
				break;
			}
		}
		if(flag==0)
			printf("N/A\n");
		
	}
	return 0;

}


 

你可能感兴趣的:(pat 1012 The Best Rank)