| Real Questions | Practice Questions | Syllabus Analysis |
|---|---|---|
| Level 1 Real Questions List | Level 1 Practice Questions List | Level 1-5 Syllabus Analysis |
| Level 2 Real Questions List | Level 2 Practice Questions List | Essential Skills for GESP/CSP Programming |
| Level 3 Real Questions List | Level 3 Practice Questions List | |
| Level 4 Real Questions List | Level 4 Practice Questions List | |
| Level 5 Real Questions List | Level 5 Practice Questions List |
| CSP-XL |
|---|
| 2025 Liaoning CSP-XL Semi-Final Real Questions Analysis |
GESP C++ Level 3 Exercise, one-dimensional array practice, difficulty ★★☆☆☆.
luogu-P5728: Comparable Opponents
Problem Requirements
Problem Description
There are students participating in the final exam, and we have the information of each student: scores in Chinese, Mathematics, and English (all are natural numbers not exceeding ). If the score difference in each subject between a pair of students does not exceed , and the total score difference does not exceed , then this pair of students is considered “comparable opponents”. Now we want to know how many pairs of “comparable opponents” exist among these students? The same person may pair with several other students.
Input Format
The first line contains a positive integer .
Next, lines follow, each containing three integers, where the line represents the scores of the student in Chinese, Mathematics, and English. The first student read in is numbered as .
Output Format
Output a single integer representing the number of pairs of “comparable opponents”.
Input Output Example #1
Input #1
3
90 90 90
85 95 90
80 100 91
Output #1
2
Explanation/Hint
The data guarantees that and each subject score does not exceed .
Problem Analysis
Solution Approach
The solution approach for this problem is as follows:
-
Problem Analysis:
- Input the total number of students ().
- Each student has three subject scores: Chinese, Mathematics, and English (all are natural numbers not exceeding 150).
- Need to find the number of pairs of “comparable opponents”.
Solution Method:
- Core Idea:
- Use three arrays to store the scores of the three subjects.
- Use a double loop to iterate through all possible pairs of students.
- Check if each pair of students meets the “comparable” conditions.
- Implementation:
- Read N and all student scores.
- Iterate through all possible student pairs (i,j), where j>i to avoid duplicates.
- Check if the pair of students meets the conditions and count.
- Implementation Points:
- Conditions for comparability:
- Score difference in each subject does not exceed 5 points.
- Total score difference does not exceed 10 points.
- Use absolute value function to calculate score differences.
- Note that the array size should be defined based on the input N.
Note: This problem can be solved using a two-dimensional array to store all students’ scores, and then using a double loop to iterate through all possible student pairs. However, considering that a two-dimensional array is not within the scope of the three-level syllabus, we specifically use three one-dimensional arrays for implementation.
Complexity Analysis:
- Time Complexity: , where N is the total number of students.
- Space Complexity: , requires storage for N students’ three subject scores.
Example Code
#include <cmath>
#include <iostream>
int main() {
// Read the number of students N
int N;
std::cin >> N;
// Define three arrays to store scores in Chinese, Mathematics, and English
int one_ary[N]; // Chinese scores array
int two_ary[N]; // Mathematics scores array
int three_ary[N]; // English scores array
// Read each student's three subject scores
for (int i = 0; i < N; i++) {
std::cin >> one_ary[i] >> two_ary[i] >> three_ary[i];
}
// Count the number of comparable opponents
int count = 0;
// Double loop to iterate through all possible student pairs
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
// Check if they are comparable opponents:
// 1. Score difference in each subject does not exceed 5 points
// 2. Total score difference does not exceed 10 points
if (std::abs(one_ary[j] - one_ary[i]) <= 5 &&
std::abs(two_ary[j] - two_ary[i]) <= 5 &&
std::abs(three_ary[j] - three_ary[i]) <= 5 &&
std::abs(one_ary[i] + two_ary[i] + three_ary[i] -
(one_ary[j] + two_ary[j] + three_ary[j])) <= 10) {
count++;
}
}
}
// Output result
std::cout << count << std::endl;
return 0;
}
【Recommended】Mobile Version:【GESP】C++ Certification Learning Resource Summary
【Recommended】Desktop Version: GESP/CSP Examination Material Website:https://wiki.coderli.com/ Dictionary-style resource organization, easy and quick to browse by category.

“luogu-” series problems can be evaluated online atLuogu Problem Bank.
“bcqm-” series problems can be evaluated online atProgramming Enlightenment Problem Bank.