跳到正文

个人记录

课堂笔记

obbycode约 3 分钟
原文

这是 2026年11月15日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年11月15日
收录
2026年11月15日
更新
2026年11月22日
篇幅
约 3 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1
C++
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1020;          // 最多 1000 名学生,开 1020 留余量

int n;                          // 学生人数
int ans;                        // 答案:旗鼓相当的对手对数

// 学生结构体:三科成绩 + 总分
struct student{
    int chinese;                // 语文
    int math;                   // 数学
    int english;                // 英语
    int total;                  // 总分 = 语文 + 数学 + 英语
};

struct student a[MAXN];         // 结构体数组,a[1..n] 存每名学生信息

// 判断两个数之差是否不超过 z(即 |x - y| <= z)
// 单科调用时 z = 5,总分调用时 z = 10
bool check(int x, int y, int z){
    return abs(x - y) <= z;     // abs 求绝对值
}

int main(){
    cin >> n;

    // 读入每名学生的三科成绩,并计算总分
    for(int i = 1; i <= n; i++){
        cin >> a[i].chinese >> a[i].math >> a[i].english;
        a[i].total = a[i].chinese + a[i].math + a[i].english;
    }

    // 枚举所有学生对 (i, j),i < j 保证每对只统计一次
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            // 三科分差都不超过 5,且总分分差不超过 10 → 旗鼓相当的对手
            if(check(a[i].chinese, a[j].chinese, 5)
               && check(a[i].math,    a[j].math,    5)
               && check(a[i].english, a[j].english, 5)
               && check(a[i].total,   a[j].total,  10))
            {
                ans++;          // 找到一对,计数加一
            }
        }
    }

    cout << ans;                // 输出对手总对数
    return 0;
}