#include<stdio.h>
#include<math.h>
#include<conio.h>
struct scoresort
{
int score;
int position;
};
void input(struct scoresort score[10]);
void sort(struct scoresort score[10]);
float avg(struct scoresort score[10]);
int unjustice(struct scoresort score[10],float avg,int position[10]);
int justice(struct scoresort score[10],float avg,int position[10]);
void input(struct scoresort score[10])
{
int i;
for(i=0;i<10;i++)
{
int flag=0;
int s;
printf("please input the score of %d:",i+1);
scanf("%d",&s);
while(flag==0)
{
if(s>=0&&s<=100)
{
score[i].score=s;
score[i].position=i+1;
flag=1;
}
else
{
printf("input error!\n");
printf("please input the score of %d:",i+1);
scanf("%d",&s);
}
}
}
}
void sort(struct scoresort score[10])
{
int s;
int p;
int i;
for(i=0;i<9;i++)
{
int j;
for(j=i+1;j<10;j++)
{
if(score[i].score>score[j].score)
{
s=score[i].score;
p=score[i].position;
score[i].score=score[j].score;
score[i].position=score[j].position;
score[j].score=s;
score[j].position=p;
}
}
}
}
float avg(struct scoresort score[10])
{
int num=0;
int i;
for(i=1;i<9;i++)
{
num+=score[i].score;
}
return (float)num/8;
}
int unjustice(struct scoresort score[10],float avg,int position[10])
{
int p=0;
int i;
if((avg-score[0].score)<(score[9].score-avg))
{
position[p]=score[9].position;
p++;
i=8;
while(score[9].score==score[i].score)
{
position[p++]=score[i].position;
i--;
}
}
if((avg-score[0].score)>(score[9].score-avg))
{
position[p++]=score[0].position;
i=1;
while(score[0].score==score[i].score)
{
position[p++]=score[i].position;
i++;
}
}
else
{
position[p++]=score[0].position;
position[p++]=score[9].position;
i=8;
while(score[9].score==score[i].score)
{
position[p++]=score[i].position;
i--;
}
i=1;
while(score[0].score==score[i].score)
{
position[p++]=score[i].position;
i++;
}
}
return p;
}
int justice(struct scoresort score[10],float avg,int position[10])
{
int p=0;
int start=4;
int high;
int low;
int i;
while((score[start].score-avg)>=0)
{
start--;
}
while((score[start].score-avg)<=0)
{
if((score[start].score-avg)<0)
low=start;
start++;
}
high=start;
if(high!=low+1)
{
for(i=low+1;i<high;i++)
position[p++]=score[i].position;
return p;
}
else
{
if((avg-score[low].score)>(score[high].score-avg))
{
position[p++]=score[high].position;
while(score[high].score==score[high+1].score)
{
position[p++]=score[high+1].position;
high++;
}
}
else if((avg-score[low].score)<(score[high].score))
{
position[p++]=score[low].position;
while(score[low].score==score[low-1].score)
{
position[p++]=score[low-1].position;
low--;
}
}
else
{
position[p++]=score[low].position;
while(score[low].score==score[low-1].score)
{
position[p++]=score[low-1].position;
low--;
}
position[p++]=score[high].position;
while(score[high].score==score[high+1].score)
{
position[p++]=score[high+1].position;
high++;
}
}
return p;
}
}
void main()
{
float avgscore;
int ujp[10];
int jp[10];
int ujnum;
int jnum;
int i;
struct scoresort score[10];
input(score);
for(i=0;i<10;i++)
printf("%d ",score[i].score);
printf("\n");
sort(score);
for(i=0;i<10;i++)
printf("%d ",score[i].score);
printf("\n");
avgscore=avg(score);
printf("the average score is:%f",avgscore);
ujnum=unjustice(score,avgscore,ujp);
printf("\nthe most unjustice is:");
for(i=0;i<ujnum;i++)
printf("%d ",ujp[i]);
jnum=justice(score,avgscore,jp);
printf("\nthe most justice is:");
for(i=0;i<jnum;i++)
printf("%d ",jp[i]);
getch();
}