PTA Ladder Competition L2-015 Mutual Evaluation Results C++ Realization

The simple rules for students to evaluate each other's homework are as follows: each person's homework will be kreviewed by a classmate and get ka grade. The system needs to remove the highest score and the lowest score, and average the remaining scores to get the student's final score. This question requires you to write the scoring module of this mutual evaluation system.

Input format:

Enter the first line to give 3 positive integers N(3 < N≤ 104, the total number of students), k(3 ≤ k≤ 10, the number of reviews for each assignment), M(≤ 20, the number of students to be output). Subsequent lines, each line gives the assessment grades (in the interval [0, 100]) Nobtained by an assignment , separated by spaces.k

Output format:

Output Mthe final score with the highest score in non-decreasing order, retaining 3 decimal places. There is 1 space between the scores, and there must be no extra spaces at the beginning and end of the line.

Input sample:

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

Sample output:

87.667 88.000 96.000

Problem-solving ideas:

A simulation would suffice, but in order to highlight the current state of the art, I'll take an unusual approach to this problem.

Tips: When m is less than 0, nothing needs to be output.

Code example:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, k ,m;
    cin >> n >> k >> m;
    
    vector<double> ans; //存储每次的平均成绩
    while(n--)
    {
        multiset<double> res; //每次都存k个成绩 本容器会将其中的数据升序排序
        for(int i = 0; i < k; i++)
        {
            double temp;
            cin >> temp;
            res.insert(temp);
        }
        
        double sum = 0; //求和
        auto p = res.begin(); //得到本容器的第一个数据的位置
        p++; //移动到下一个位置
        
        auto q = res.end(); //得到本容器的最后一个数据的位置的下一个位置
        q--; //移动到上一个位置
        
        for(; p != q; p++) //掐头去尾求和
        {
            sum += *p;
        }
        
        ans.emplace_back(sum / (k - 2)); //存平均数
    }
    
    sort(ans.begin(), ans.end(), greater<double>()); //将ans中的数据降序排序
    
    //输出控制
    if(m != 0)
    {
        printf("%.3f", ans[m - 1]);
        for(int i = m - 2; i >= 0; i--)
        {
            printf(" %.3f", ans[i]);
        }
    }

    return 0;
}

operation result:

Guess you like

Origin blog.csdn.net/m0_53209892/article/details/123556889