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

本文介绍了一种解决PATest竞赛中特定问题的算法实现,该算法通过排除最大和最小值来计算每组数的平均值,并使用自定义排序进行结果输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

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

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

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值