有一个小小的问题。希望大神搭把手~~~
在一个大赛中,有10个评委为参赛选手打分,分数为1-100分。选手最后得分为:去掉一个最高分,去掉一个最低分,其余八个分数的平均值,为这个选手的最后得分。
根据这个得分,从所有的选手中选出前三名。输出名字和成绩。
(这个问题怎么用数据结构来完成,
大神能提供代码最好的 提供80%的积分)
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #define ArraySize 5 struct grade{ int data; struct grade *lch; struct grade *rch; }; struct PLAYER{ int num; char name[20]; double ave; struct grade *score; }; struct grade *ADD_SCORE_TO_PLAYER(struct grade *rt,int data){ int FLAG=0; struct grade *p,*t; p=(struct grade *)malloc(sizeof(struct grade)); p->data=data; p->lch=p->rch=NULL; if(rt==NULL) rt=p; else{ t=rt; while(!FLAG) if(t->data>data){if(t->lch==NULL){t->lch=p;FLAG=1;}else t=t->lch;} else{if(t->rch==NULL){t->rch=p;FLAG=1;}else t=t->rch;} } return rt; } void GET_SUM(struct grade *sc,int &sum){ if(sc){ GET_SUM(sc->lch,sum); sum+=sc->data; printf("%d\t",sc->data); GET_SUM(sc->rch,sum); } } void SORT(struct PLAYER py[],int n){ for(int i=0;i<n-1;i++){ for(int j=n-2;j>=i;j--) if(py[j].ave<py[j+1].ave){ double temp=py[j].ave; py[j].ave=py[j+1].ave; py[j+1].ave=temp; } } } main() { int i,j,n=10,sc,sum; struct PLAYER Array[ArraySize]; for(i=0;i<ArraySize;i++){ Array[i].score=(struct grade *)malloc(sizeof(struct grade)); Array[i].score=NULL; } for(i=0;i<ArraySize;i++){ printf("ADD name TO PLAYER %d:\n",i+1); // scanf("%s",Array[i].name); Array[i].num=i+1; Array[i].ave=0; printf("ADD scores TO PLAYER %d:\n",i+1); for(j=0;j<n;j++){ printf("ADD score[%d]\t",j+1); // scanf("%d",&sc); sc=i*10+j+5; Array[i].score=ADD_SCORE_TO_PLAYER(Array[i].score,sc); } } for(i=0;i<ArraySize;i++){ sum=0; GET_SUM(Array[i].score,sum); struct grade *s=Array[i].score; if(s->lch){ while(s->lch)s=s->lch; sum-=s->data; } else sum-=s->data; s=Array[i].score; if(s->rch){ while(s->rch)s=s->rch; sum-=s->data; } else sum-=s->data; Array[i].ave=1.0*sum/8; } SORT(Array,ArraySize); printf("RESULTS:\n"); for(i=0;i<3;i++){ // printf("PLAYER%d:%s",i+1\t,Array[i].name); printf("PLAYER%d:%.2lf\n",i+1,Array[i].ave); } getch(); }