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

题目链接

https://www.patest.cn/contests/gplt/L2-015

思路
在求和的过程中 标记一下 最大值和最小值,在最后求平均的时候 用总和减去最大值和最小值 去除 (总数 - 2) 然后最后排序的时候 先按升序来排 然后 最后 POP 掉 多余的 再按升序来排

AC代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef long long LL;

const double PI  = 3.14159265358979323846264338327;
const double E   = 2.718281828459;
const double eps = 1e-6;

const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e6 + 5;
const int MOD  = 1e9 + 7;

bool comp(double x, double y)
{
    return x > y;
}

int main()
{
    int n, k, m;
    scanf("%d%d%d", &n, &k, &m);
    vector <double> v;
    v.clear();
    for (int i = 0; i < n; i++)
    {
        int Min = MAXN, Max = MINN;
        double sum = 0;
        int num;
        for (int j = 0; j < k; j++)
        {
            scanf("%d", &num);
            Min = min(Min, num);
            Max = max(Max, num);
            sum += num;
        }
        sum = (sum - Min - Max) * 1.0 / (k - 2);
        v.push_back(sum);
    }
    sort(v.begin(), v.end(), comp);
    while (v.size() > m)
        v.pop_back();
    sort(v.begin(), v.end());
    vector <double>::iterator it;
    int i;
    for (it = v.begin(), i = 0; it != v.end() && i < m; i++, it++)
    {
        if (i)
            printf(" ");
        cout << setprecision(3) << std::fixed <<  (*it);
    }
}

你可能感兴趣的:(PAT,天梯赛)