题目简述
题意
有 $N$ 个学生的名字和三科成绩,现在需要求出每一对满足以下要求的学生:
- 每一科的成绩相差都不超过 $5$。
- 总分相差不超过 $10$。
并输出每队旗鼓相当的学生。
思路
每组学生的名字都是按字典序排的,所以我们直接暴力枚举 $i$ 和 $j$,如果学生 $i$ 和学生 $j$ 满足上述两个要求,我们就直接输出。
为了使代码简便,我们可以将这两个条件判断写在一个函数里,如果符合要求,就直接输出。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <bits/stdc++.h> using namespace std; #define int long long const int N = 1e6 + 5; int n, d = 0; int x, y, z, sum;
struct fish { int yw, sx, en; int ans; string na; }a[N];
bool pan(int j) { return (abs(a[j].yw - x) <= 5 && abs(a[j].sx - y) <= 5 && abs(a[j].en - z) <= 5 && abs(a[j].ans - sum) <= 10); }
signed main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].na >> a[i].yw >> a[i].sx >> a[i].en; a[i].ans = a[i].yw + a[i].sx + a[i].en; } for (int i = 0; i < n; i++) { x = a[i].yw, y = a[i].sx, z = a[i].en, sum = a[i].ans; for (int j = i + 1; j < n; j++) { if (pan(j)) { cout << a[i].na << " " << a[j].na << "\n"; } } } return 0; }
|