PAT Basic Level 1015
AC代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct stu 6 { 7 char stuNo[10]; 8 int moralScore; 9 int culturalScore; 10 int sum ; 11 int type; 12 }; 13 int compar(const void *a,const void *b); 14 15 int main () 16 { 17 int amount; 18 int L; 19 int H; 20 int cnt = 0; 21 struct stu *stu; 22 scanf("%d %d %d",&amount,&L,&H); 23 stu = malloc(amount * sizeof(*stu)); 24 int i = 0; 25 for(i = 0;i < amount;i++) 26 { 27 scanf("%s %d %d",&stu[i].stuNo,&stu[i].moralScore,&stu[i].culturalScore); 28 } 29 for(i = 0;i < amount;i++) //求出德才分之和 30 { 31 stu[i].sum = stu[i].moralScore+stu[i].culturalScore; 32 } 33 for(i = 0;i < amount;i++) 34 { 35 if(stu[i].moralScore >= H&&stu[i].culturalScore >= H) 36 { 37 stu[i].type = 1; 38 cnt++; 39 }else if (stu[i].moralScore >= H &&(stu[i].culturalScore < H&&stu[i].culturalScore >= L)) 40 { 41 stu[i].type = 2; 42 cnt++; 43 }else if ((stu[i].moralScore < H&&stu[i].moralScore >= L)&&(stu[i].culturalScore < H&&stu[i].culturalScore >= L)&&stu[i].moralScore >= stu[i].culturalScore) 44 { 45 stu[i].type = 3; 46 cnt++; 47 }else if ((stu[i].moralScore >= L)&&(stu[i].culturalScore >= L)) 48 { 49 stu[i].type = 4; 50 cnt++; 51 } 52 } 53 qsort(stu,amount,sizeof(*stu),compar); //为什么前面加VOID错误??!! 54 printf("%d\n",cnt); 55 for(i = 0;i < amount ;i++) 56 { 57 if(stu[i].type >= 1&&stu[i].type <= 4&&i != amount - 1) 58 printf("%s %d %d\n",stu[i].stuNo,stu[i].moralScore,stu[i].culturalScore); 59 else if(stu[i].type >= 1&&stu[i].type <= 4&&i == amount - 1) 60 printf("%s %d %d",stu[i].stuNo,stu[i].moralScore,stu[i].culturalScore); 61 } 62 63 64 return 0; 65 66 } 67 int compar(const void *a,const void *b) 68 { 69 struct stu s1 = *(struct stu *)a; 70 struct stu s2 = *(struct stu *)b; 71 if(s1.type == s2.type) 72 { 73 if(s1.sum == s2.sum) 74 { 75 if(s1.moralScore == s2.moralScore) 76 { 77 return strcmp(s1.stuNo,s2.stuNo); 78 79 }else 80 return s2.moralScore - s1.moralScore; 81 }else 82 return s2.sum - s1.sum; 83 }else 84 return s1.type - s2.type; 85 }