1012. The Best Rank (25)
2013-07-09 17:51
309 查看
哪里不对??
有重复时注意排名顺序。 1 2 3 3 5 不是 1 2 3 3 4 。。。。。
有重复时注意排名顺序。 1 2 3 3 5 不是 1 2 3 3 4 。。。。。
#include<stdio.h> typedef struct tagStudent { char id[7]; int grades[4]; //acme int ranks[4]; //acme }Student; typedef int (*CmpFun)(const void *a,const void *b); int cmpa(const void *a ,const void *b) { Student* sa = (Student*)a; Student* sb = (Student*)b; return sb->grades[0] - sa->grades[0]; } int cmpc(const void *a ,const void *b) { Student* sa = (Student*)a; Student* sb = (Student*)b; return sb->grades[1] - sa->grades[1]; } int cmpm(const void *a ,const void *b) { Student* sa = (Student*)a; Student* sb = (Student*)b; return sb->grades[2] - sa->grades[2]; } int cmpe(const void *a ,const void *b) { Student* sa = (Student*)a; Student* sb = (Student*)b; return sb->grades[3] - sa->grades[3]; } int cmpid(const void *a ,const void *b) { Student* sa = (Student*)a; Student* sb = (Student*)b; return strcmp(sa->id,sb->id); } int bsearch(Student stu[],int begin,int end,char* target) { while(begin<=end) { int middle = begin+(end-begin)/2; int flag = strcmp(stu[middle].id,target); if(flag==0) return middle; else if(flag==1) end = middle - 1; else begin = middle + 1; } return -1; } char map[] = {'A','C','M','E'}; CmpFun cmps[] = {cmpa,cmpc,cmpm,cmpe}; int main() { int n,m; int i,j; Student stu[2000]; char ids[2000][7]; int prevgrade; 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]= (stu[i].grades[1]+stu[i].grades[2]+stu[i].grades[3])/3; } for(i=0;i<m;i++) scanf("%s",ids[i]); for(i=0;i<4;i++) { prevgrade = -1; qsort(stu,n,sizeof(Student),cmps[i]); for(j=0;j<n;j++) { if(stu[j].grades[i]!=prevgrade) { stu[j].ranks[i] = j+1; prevgrade = stu[j].grades[i]; } else stu[j].ranks[i] = stu[j-1].ranks[i]; } } qsort(stu,n,sizeof(Student),cmpid); for(i=0;i<m;i++) { int pos = bsearch(stu,0,n-1,ids[i]); if(pos==-1) printf("N/A\n"); else { int rank_kind = 0; for(j=1;j<4;j++) { if(stu[pos].ranks[j]<stu[pos].ranks[rank_kind]) rank_kind = j; } printf("%d %c\n",stu[pos].ranks[rank_kind],map[rank_kind]); } } return 0; }
相关文章推荐
- 1012. The Best Rank (25)
- PAT 1012. The Best Rank (25) 一定范围的整数值排序
- PAT 1012. The Best Rank (25)
- 1012. The Best Rank (25)
- 1012. The Best Rank (25)
- 1012 The Best Rank (25)
- 1012. The Best Rank (25)
- 1012. The Best Rank (25)暴力枚举 排序
- PAT (Advanced Level) 1012. The Best Rank (25)
- 1012. The Best Rank (25)
- 1012. The Best Rank (25)
- [PAT]1012. The Best Rank (25)
- 1012. The Best Rank (25)
- PATA-1012-The Best Rank (25)
- PAT甲 1012. The Best Rank (25)
- PAT (Advanced) 1012. The Best Rank (25)
- PAT 1012. The Best Rank (25) 数据结构,排序
- 浙大PAT (Advanced Level) Practise 1012 The Best Rank (25)
- PAT 1012. The Best Rank (25)
- PAT 1012. The Best Rank (25)