GESP Level 1 practice, conditions and logic, beginner difficulty.
-
CCF-GESP C++ Assessment Standards
Hongyang, WeChat Official Account: Hongyang’s Programming Class CCF-GESP C++ Assessment Standards
Comprehensive Guide – [GESP2303 Level 1] Rectangle Area
Problem Requirements
Problem Description
Little Ming has just learned how to calculate the area of a rectangle. He found that if the length and width of a rectangle are both integers, its area must also be an integer. Now, Little Ming wants to know how many possible rectangles can be formed given the area of the rectangle, such that both the length and width are integers.
If two rectangles have equal lengths and widths, they are considered the same rectangle. It is agreed that the length of the rectangle is greater than or equal to the width. A square is a special case of a rectangle, where the length and width can be equal.
Input Format
Input a line containing an integerA, representing the area of the rectangle. It is agreed that2 ≤ A ≤ 1000
Output Format
Output a line containing an integerc, representing the number of possible rectangles.
Input and Output Examples
Input #1
4
Output #1
2
Input #2
6
Output #2
2
Explanation/Hint
【Example Explanation 1】
There are 2 types of rectangles with an area of 4, with dimensions of2×2 and 4×1.
【Example Explanation 2】
There are 2 types of rectangles with an area of 6, with dimensions of3×2 and 6×1.
Problem Analysis
Solution Approach
By iterating through all possible lengths <span>i</span> (from 1 to √s), for each <span>i</span>:
-
Calculate the corresponding width
<span>j = s / i</span>(integer division) -
Check if
<span>i * j == s</span>holds (to ensure divisibility) -
If it holds, it indicates a valid length-width combination has been found, increment the counter by 1
Complexity Analysis:
- Time complexity is O(√s)
- Space complexity is O(1)
Example Code
#include<stdio.h> int main() { int s, ans = 0; // s: input area value, ans: counter for possible rectangles scanf("%d", &s);// read area value from standard input // Iterate through all possible lengths i (from 1 to √s) for(int i = 1; i * i <= s; i++) { int j = s / i; // Calculate corresponding width j // Check if i*j equals s (to ensure divisibility) if(i * j == s) { ans++; // If condition is met, increment counter } } printf("%d", ans); // Output the number of possible rectangles return 0;}
“Comprehensive Guide–” series problems can be evaluated online atInformatics Olympiad Comprehensive Guide (C++ Version).