7-2 互评成绩 (25 分)
学生互评作业的简单规则是这样定的:每个人的作业会被k个同学评审,得到k个成绩。系统需要去掉一个最高分和一个最低分,将剩下的分数取平均,就得到这个学生的最后成绩。本题就要求你编写这个互评系统的算分模块。
输入格式:
输入第一行给出3个正整数N(3 < N ≤10
4
,学生总数)、k(3 ≤ k ≤ 10,每份作业的评审数)、M(≤ 20,需要输出的学生数)。随后N行,每行给出一份作业得到的k个评审成绩(在区间[0, 100]内),其间以空格分隔。
输出格式:
按非递减顺序输出最后得分最高的M个成绩,保留小数点后3位。分数间有1个空格,行首尾不得有多余空格。
输入样例:
6 5 3
88 90 85 99 60
67 60 80 76 70
90 93 96 99 99
78 65 77 70 72
88 88 88 88 88
55 55 55 55 55
输出样例:
87.667 88.000 96.000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
double num;
}p[10005];
int partition(struct node a[],int low,int high)
{
double x=a[low].num;
while(low<high)
{
while(low<high&&a[high].num>=x)high--;
a[low].num=a[high].num;
while(low<high&&a[low].num<=x) low++;
a[high].num=a[low].num;
}
a[low].num=x;
return low;
}
void kuaipai(struct node a[],int left,int right)
{
int m;
if(left<right)
{
m=partition(a,left,right);
kuaipai(a,left,m-1);
kuaipai(a,m+1,right);
}
}
int main()
{
int i,j,n,k,m,max,min;
int x,sc;
scanf("%d%d%d",&n,&k,&m);
for(i=0;i<n;i++)
{
max=-1;min=999;sc=0;
for(j=0;j<k;j++)
{
scanf("%d",&x);
if(x>max)max=x;
if(x<min)min=x;
sc+=x;
}
p[i].num=1.0*(sc-max-min)/(1.0*(k-2));
}
kuaipai(p,0,n-1);
for(i=n-m;i<n-1;i++)
printf("%.3lf ",p[i].num);
printf("%.3lf\n",p[n-1].num);
return 0;
}
C++ 调用sort函数
bool cmp(node a,node b)
{
return a.num>b.num;
}
sort(p,p+n,cmp);