Đề bài
A rectangle has two sides of length width and two sides of length height. Its perimeter is the total length of all four sides.
Implement rectangle_perimeter(width, height).
Input
width: The width of the rectangle. height: The height of the rectangle.
Output
Return the perimeter as an integer.
Ràng buộc
1 ≤ width, height ≤ 1,000,000
Ví dụ
Đầu vào:
width = 3
height = 5
Kết quả mong đợi:
expected = 16
Đầu vào:
long long width = 3;
long long height = 5;
Kết quả mong đợi:
long long expected = 16;
Đầu vào:
long width = 3;
long height = 5;
Kết quả mong đợi:
long expected = 16;
Giải thích
Two sides of 3 and two sides of 5: 3 + 3 + 5 + 5 = 16.
Đầu vào:
width = 7
height = 7
Kết quả mong đợi:
expected = 28
Đầu vào:
long long width = 7;
long long height = 7;
Kết quả mong đợi:
long long expected = 28;
Đầu vào:
long width = 7;
long height = 7;
Kết quả mong đợi:
long expected = 28;
Giải thích
A square is a rectangle too: 4 × 7 = 28.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
A rectangle has two equal widths and two equal heights.
Gợi ý 2
Add all four sides, or combine the two repeated side lengths first.
Gợi ý 3
Watch for integer arithmetic only; no special cases are needed here.
Độ phức tạp mong đợi
O(1) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.