[문제 위치]
https://www.acmicpc.net/problem/15687
[문제 풀이]
클래스 구현 문제이다.
특별한 알고리즘이 들어가는 것은 아니지만 클래스 개념에 따라 주어진 내용을 충실히 구현하면 된다.
#include <iostream>
class Rectangle {
private:
int width;
int height;
public:
// 생성자
Rectangle(int w, int h) {
width = w;
height = h;
}
// 가로 길이 반환
int get_width() const {
return width;
}
// 세로 길이 반환
int get_height() const {
return height;
}
// 가로 길이 설정 (유효성 검사 포함)
void set_width(int w) {
if (w > 0 && w <= 1000)
width = w;
}
// 세로 길이 설정 (유효성 검사 포함)
void set_height(int h) {
if (h > 0 && h <= 2000)
height = h;
}
// 넓이 계산
int area() const {
return width * height;
}
// 둘레 계산
int perimeter() const {
return 2 * (width + height);
}
// 정사각형 여부
bool is_square() const {
return width == height;
}
};'문제 풀이 > 문제 풀이(BOJ)' 카테고리의 다른 글
| [Silver V] 카약 - 1380 (0) | 2025.08.12 |
|---|---|
| [Silver V] K-세준수 (0) | 2025.08.10 |
| [Silver V] 돌려 돌려 돌림판! (3) | 2025.08.08 |
| [Silver V] 젓가락 - 24228 (0) | 2025.08.06 |
| [Silver V] 연도 진행바 - 1340 (1) | 2025.08.05 |