2017年pat乙级冬考B题互评成绩计算 20分 题型:模拟

91 阅读2分钟

1077 互评成绩计算 - PAT (Basic Level) Practice (中文) (pintia.cn)

思想

先把导师的评分保留,留到最后用,然后把其他组的评分加上之后最后去掉最大最小评分求出平均值,最后再加上之前的导师评分/2就可以得出结果了。

ps

注意精度问题,/2会有浮点数产生,所以要先*1.0,然后最后再四舍五入+0.5,最后还要转为整形

#include <iostream>
using namespace std;
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {

        //每一轮都要重新初始化
        int head, sum = 0, cnt = -2, temp, maxn = -1, minn = m + 1;  //需要排除掉最高分和最低分,所以初始化为-2是为了排除掉这两个极端值。
        cin >> head; //保存导师的评分
        for (int j = 0; j < n - 1; j++) {  //从第二个元素开始到最后一个元素都是别的组的评分
            cin >> temp;
            if (temp >= 0 && temp <= m) {
                if (temp > maxn) maxn = temp;
                if (temp < minn) minn = temp;
                sum += temp;
                cnt++;
            }
        }

        //四舍五入
        cout << int((((sum - minn - maxn) * 1.0 / cnt) + head) / 2 + 0.5) << endl;
    }
    return 0;
}

image.png

第二种表述方式 不容易错

#include<iostream>
using namespace std;


int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {

        double head, sum = 0;  //用double型存 评委的分 和 其他组评分和  后期不用再*1.0转double



           int cnt = 0, temp, max_n = -999, min_n = 999;  //分数均不超过100,所以设为三位数就可以了
        cin >> head; //保存导师的评分

        for (int j = 0; j < n - 1; j++) {  //从第二个元素开始到最后一个元素都是别的组的评分
            cin >> temp;
            if (temp >= 0 && temp <= m) {
                if (temp > max_n) max_n = temp;
                if (temp < min_n) min_n = temp;
                sum += temp;
                cnt++;
            }
        }

        sum = (sum - min_n - max_n) / (cnt-2);  
        int ans = (sum + head) / 2 + 0.5;  //最后的结果经过经过四舍五入是一个整形,因此我们用整形来存
        cout << ans << endl;
 
    }
    return 0;
}

image.png