PTA天梯赛L2-015 互评成绩【排序】

学生互评作业的简单规则是这样定的:每个人的作业会被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<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include <iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
int a[12];
double c[maxn];
int main()
{
    int n, k, m;
    scanf("%d%d%d", &n, &k, &m);
    for(int i = 0; i < n; ++i)
    {
        int Max = -1e9, Min = inf;
//        int pos_a = k, pos_b = k;
        int sum = 0;
        for(int j = 0; j < k; ++j)
        {
            scanf("%d", &a[j]);
            sum += a[j];
            if(a[j] > Max)
            {
                Max = a[j];
//                pos_a = j;
            }
            if(a[j] < Min)
            {
                Min = a[j];
//                pos_b = j;
            }
        }
        sum = sum - Max - Min;
        c[i] = sum * 1.0 / (k - 2);
    }
    sort(c, c + n);
    for(int i = n - m; i < n - 1; ++i)
        printf("%0.3lf ", c[i]);
    printf("%0.3lf", c[n - 1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/88650358