基础实验7-2.3 德才论 (25分)--排序
解题思路:主要是依题意,写好排序规则(先按类型降序,再按总分降序,总分相同则按德分降序,德分相同按准考证号升序)
#include <stdio.h> typedef struct { char ID[9]; int DScore; int CScore; int sum; int type; } Stu_Info; int cmp(Stu_Info a,Stu_Info b) { if(a.type==b.type) { if(a.sum==b.sum) { if(a.DScore==b.DScore) return strcmp(b.ID,a.ID); return a.DScore-b.DScore; } return a.sum-b.sum; } return a.type-b.type; } int Partition(Stu_Info stu[],int low,int high) { Stu_Info pivot=stu[low]; while(low<high) { while(low<high) { if(cmp(stu[high],pivot)>0) { stu[low]=stu[high]; break; } high--; } while(low<high) { if(cmp(stu[low],pivot)<0) { stu[high]=stu[low]; break; } low++; } } stu[low]=pivot; return low; } void Sort(Stu_Info stu[],int low,int high) { if(low<high) { int t=Partition(stu,low,high); Sort(stu,low,t-1); Sort(stu,t+1,high); } } int main() { int N,L,H; scanf("%d %d %d",&N,&L,&H); int i,cnt=0; Stu_Info stu[N]; for(i=0; i<N; i++) { scanf("%s %d %d",stu[i].ID,&stu[i].DScore,&stu[i].CScore); stu[i].sum=stu[i].CScore+stu[i].DScore; if(stu[i].CScore>=H) {//才分不小于H if(stu[i].DScore>=H)//德分不小于H 第一类 stu[i].type=5; else if(stu[i].DScore>=L)//德分不小于L stu[i].type=2; else { stu[i].type=1; cnt++; } } else if(stu[i].CScore>=L) {//才分小于H,不小于L if(stu[i].DScore>=H)//德分不小于H 第二类 stu[i].type=4; else if(stu[i].DScore>=L) {//L=<德分<H if(stu[i].DScore>=stu[i].CScore)//德分不小于才分 第三类 stu[i].type=3; else stu[i].type=2; } else { stu[i].type=1; cnt++; } } else { stu[i].type=1; cnt++; } } Sort(stu,0,N-1); printf("%d\n",N-cnt); for(i=0; i<N; i++) { if(stu[i].type!=1) printf("%s %d %d\n",stu[i].ID,stu[i].DScore,stu[i].CScore); } }
勤能补拙,熟能生巧