|
U
Pro17. Rectangle Area, Perimeter, and Comparison
Easy
Problem Description
Given the width and height of two rectangles R1 and R2:
- •Compute the area and perimeter of R1.
- •Compute the area and perimeter of R2.
- •Compare the two rectangles by area.
Input Format
- •Line 1: Two space-separated integers w1 and h1 (dimensions of R1).
- •Line 2: Two space-separated integers w2 and h2 (dimensions of R2).
Output Format
- •Line 1: Two space-separated integers: Area and Perimeter of R1.
- •Line 2: Two space-separated integers: Area and Perimeter of R2.
- •Line 3:
R1if R1 has strictly greater area,R2if R2 has strictly greater area, orTieif their areas are equal.
Example 1:
Input:
4 5 3 6
Output:
20 18 18 18 R1
Explanation: R1: area = 20, perimeter = 18. R2: area = 18, perimeter = 18. R1 area (20) > R2 area (18), so R1 wins.
Example 2:
Input:
2 6 3 4
Output:
12 16 12 14 Tie
Explanation: R1: area = 12. R2: area = 12. Areas are equal, output Tie.
Example 3:
Input:
2 3 5 5
Output:
6 10 25 20 R2
Explanation: R1: area = 6. R2: area = 25. R2 has larger area.
Constraints:
1 <= w1, h1, w2, h2 <= 10^4Time complexity target: O(1)Auxiliary space target: O(1)
Follow up:How would you overload the < and == operators in C++ to compare Rectangle instances naturally?
1,180 Online
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit