GESP Level 2 practice, loops and enumerations, difficulty beginner.
-
CCF-GESP C++ Assessment Standards
Hong Yang, WeChat public account: Hong Yang’s Programming Class CCF-GESP C++ Assessment Standards
Comprehensive Guide – [GESP2506 Level 2] Counting Right Triangles
Problem Requirements
Problem Description
A right triangle has two legs and one hypotenuse. Let the lengths of the two legs be a and b, then the area of the right triangle is a*b/2.
Please calculate how many different right triangles with integer areas can be formed when the lengths of the legs a and b are positive integers not exceeding n. Two right triangles with legs a, b and a′, b′ are considered the same if and only if a=a′, b=b′ or a=b′, b=a′.
Input Format
Input a single line with an integer n, representing the maximum length of the legs.
Input a single line with an integer n, representing the maximum length of the legs.
Output Format
Output a single line with an integer n, representing the maximum length of the legs.
Input and Output Examples
Input #1
3
Output #1
5
Input #2
5
Output #2
9
Data Range
For all test cases, ensure 1≤n≤1000.
Problem Analysis
Solution Approach
-
Iterate through all possible combinations of legs (a, b) (b>=a to avoid double counting)
-
Check if the area is an integer (i.e., a × b is even)
-
Output the count
Complexity Analysis:
- Time complexity is O(n²)
- Space complexity is O(1)
Example Code
#include <bits/stdc++.h>using namespace std;int main(){ int n, ans = 0; cin >> n; // Input maximum leg length n for (int a = 1; a <= n; a++) { // Iterate through the first leg a for (int b = a; b <= n; b++) { // Iterate through the second leg b (b>=a to avoid double counting) if (a * b % 2 == 0) { // Check if the area is an integer ans++; // Count +1 } } } cout << ans << endl; // Output result return 0;}
“Comprehensive Guide–” series problems can be evaluated online atInformatics Olympiad Comprehensive Guide (C++ Version).