1085 PAT单位排行 (25分)
输入样例:
10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu
输出样例:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
思路:(struct school*)save[26][26][26] (struct school*)inf[100005]
1.学校的末3位字符(转为小写)作为save的三个下标(不足3位默认一个数字),然后将每次输入存入save中,或者加入新的,或者加分数2.首次出现的school的指针存入inf中3.对inf快排 首次通过代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 typedef struct school{ 6 char name[7]; 7 double score; 8 int student_sum; 9 struct school *next; 10 }school,*s; 11 12 s initial_s(char name[]){ 13 s s1=(s)malloc(sizeof(school)); 14 strcpy(s1->name,name); 15 s1->score=0; 16 s1->student_sum=1; 17 s1->next=NULL; 18 return s1; 19 } 20 21 void figure_score(s s1,int score,char x){ 22 if(x==‘A‘) s1->score+=(double)score; 23 else if(x==‘B‘) s1->score+=(double)score/1.5; 24 else if(x==‘T‘) s1->score+=(double)score*1.5; 25 } 26 27 int cmp(const void* a , const void* b){ 28 s a1 = *(struct school**) a; //强制类型转换 29 s b1 = *(struct school**) b; 30 int x=a1->score;int y=b1->score; 31 if(x>y) return -1; 32 else if(x==y&&a1->student_sum<b1->student_sum) return -1; 33 else if(x==y&&a1->student_sum==b1->student_sum) 34 return strcmp(a1->name,b1->name); 35 36 // return 1; 37 } 38 39 40 int main(){ 41 int sum; 42 scanf("%d",&sum); 43 s save[26][26][26]={NULL}; 44 s inf[100005]={NULL}; 45 int num=0; 46 for(int i=0;i<sum;i++){ 47 char id[7];int score;char name[7]; 48 scanf("%s %d %s",id,&score,name); 49 int x,y,z,flag=1; 50 for(int j=0;;j++){ 51 if(name[j]>=‘A‘&&name[j]<=‘Z‘) name[j]=name[j]-‘A‘+‘a‘; 52 if(name[j]==‘\0‘) break; 53 } 54 int len=strlen(name)-1; 55 x=name[len]-‘a‘; 56 len--; 57 if(len>=0&&name[len]>=‘a‘&&name[len]<=‘z‘) y=name[len]-‘a‘; 58 else y=25; 59 len--; 60 if(len>=0&&name[len]>=‘a‘&&name[len]<=‘z‘) z=name[len]-‘a‘; 61 else z=25; 62 if(save[x][y][z]==NULL){ 63 s s1=initial_s(name); 64 figure_score(s1,score,id[0]); 65 inf[num++]=s1; 66 save[x][y][z]=s1; 67 } 68 else { 69 s s2=save[x][y][z]; 70 while(s2!=NULL){ 71 if(strcmp(s2->name,name)==0){ 72 figure_score(s2,score,id[0]); 73 s2->student_sum++; 74 flag=0; 75 break; 76 } 77 s2=s2->next; 78 } 79 if(flag){ 80 s s1=initial_s(name); 81 figure_score(s1,score,id[0]); 82 inf[num++]=s1; 83 s1->next=save[x][y][z]; 84 save[x][y][z]=s1; 85 } 86 } 87 } 88 qsort(inf,num,sizeof(struct school*),cmp); 89 int num1=1; 90 int record=(int)inf[0]->score; 91 int j=0; 92 printf("%d\n",num); 93 for(int i=0;i<num;i++){ 94 if((int)inf[i]->score!=record) { 95 num1+=j; 96 j=0; 97 } 98 printf("%d %s %d %d\n",num1,inf[i]->name,(int)inf[i]->score,inf[i]->student_sum); 99 record=inf[i]->score;j++; 100 } 101 return 0; 102 }
指针数组快排参考:
FROM:https://www.cnblogs.com/nipan/p/4119714.html
原文地址:https://www.cnblogs.com/a982961222/p/12401527.html
时间: 2024-10-11 00:44:16