16. Box It!
[ 난이도: Easy | 분야: Classes ]
1. 과제
Box라는 클래스를 정의하라.
차원은 정수이고 class 내에서 private으로 정의되어 있다.
각 차원들은 레이블 되어 있다: 길이 l, breadth b, 그리고 높이 h.
클래스의 default constructordls l, b 그리고 h는 각 0으로 초기화되어 있다.
클래스의 파라미터화 constructor Box(int length, int breadth, int height)는 Box의 l, b 그리고 h(길이, breadth, 높이)를 초기화한다.
copy constructor Box(Box B)는 B의 l, b 그리고 h를 그대로 복사하여 설정한다.
위의 조건을 따라서, class는 4개의 함수가 있다:
int getLength(): box의 길이를 반환한다.
int getBreadth(): box의 breadth를 반환한다.
int getHeight(): box의 길이를 반환한다.
overload 연산자 <는 class Box에 사용한다. Box A < Box B 만약:
1. A.l < B.l
2. A.b<B.b 그리고 A.l==B.l
3. A.h<B.h 그리고 A.b==B.b 그리고 A.l==B.l
overload 연산자 <<는 class Box에 사용한다.
만약 B가 class Box의 객체라면:
cout<<B는 B.l, B.b 그리고 B.h를 단일줄에 공백으로 분리하여 출력한다.
예를 들어,
Box b1; // Should set b1.l = b1.b = b1.h = 0;
Box b2(2, 3, 4); // Should set b1.l = 2, b1.b = 3, b1.h = 4;
b2.getLength(); // Should return 2
b2.getBreadth(); // Should return 3
b2.getheight(); // Should return 4
b2.CalculateVolume(); // Should return 24
bool x = (b1 < b2); // Should return true based on the conditions given
cout<<b2; // Should print 2 3 4 in order.
제약 사항
l,b,h가 0보다 크거나 같고, 10^5보다 작거나 같다.
두 box가 < 연산자를 사용하여 비교할 때 모든 세 개의 차원이 동일하다고 할 수 없다.
문제
#include<bits/stdc++.h>
using namespace std;
//Implement the class Box
//l,b,h are integers representing the dimensions of the box
// The class should have the following functions :
// Constructors:
// Box();
// Box(int,int,int);
// Box(Box);
// int getLength(); // Return box's length
// int getBreadth (); // Return box's breadth
// int getHeight (); //Return box's height
// long long CalculateVolume(); // Return the volume of the box
//Overload operator < as specified
//bool operator<(Box& b)
//Overload operator << as specified
//ostream& operator<<(ostream& out, Box& B)
void check2()
{
int n;
cin>>n;
Box temp;
for(int i=0;i<n;i++)
{
int type;
cin>>type;
if(type ==1)
{
cout<<temp<<endl;
}
if(type == 2)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
temp=NewBox;
cout<<temp<<endl;
}
if(type==3)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
if(NewBox<temp)
{
cout<<"Lesser\n";
}
else
{
cout<<"Greater\n";
}
}
if(type==4)
{
cout<<temp.CalculateVolume()<<endl;
}
if(type==5)
{
Box NewBox(temp);
cout<<NewBox<<endl;
}
}
}
int main()
{
check2();
}
정답
#include<bits/stdc++.h>
using namespace std;
//Implement the class Box
//l,b,h are integers representing the dimensions of the box
// The class should have the following functions :
// Constructors:
// Box();
// Box(int,int,int);
// Box(Box);
// int getLength(); // Return box's length
// int getBreadth (); // Return box's breadth
// int getHeight (); //Return box's height
// long long CalculateVolume(); // Return the volume of the box
//Overload operator < as specified
//bool operator<(Box& b)
//Overload operator << as specified
//ostream& operator<<(ostream& out, Box& B)
class Box {
private:
int l; // length
int b; // breadth
int h; // height
public:
Box() {
l=0;b=0;h=0;
}
Box(int length, int breadth, int height) {
l = length; b = breadth; h = height;
}
Box(const Box& B){
l = B.l;
b = B.b;
h = B.h;
}
bool operator<(Box &ref) {
if(l < ref.l) return true;
else if((b < ref.b) && (l == ref.l)) return true;
else if((h < ref.h) && (b == ref.b) && (l == ref.l)) return true;
return false;
}
int getLength() {
return l;
}
int getBreadth() {
return b;
}
int getHeight() {
return h;
}
long long CalculateVolume() {
long long myVolume = 0;
myVolume = (long long)l * b * h;
return myVolume;
}
};
ostream& operator<<(ostream& os, Box& B){
os << B.getLength() << " " << B.getBreadth() << " " << B.getHeight();
return os;
}
void check2()
{
int n;
cin>>n;
Box temp;
for(int i=0;i<n;i++)
{
int type;
cin>>type;
if(type ==1)
{
cout<<temp<<endl;
}
if(type == 2)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
temp=NewBox;
cout<<temp<<endl;
}
if(type==3)
{
int l,b,h;
cin>>l>>b>>h;
Box NewBox(l,b,h);
if(NewBox<temp)
{
cout<<"Lesser\n";
}
else
{
cout<<"Greater\n";
}
}
if(type==4)
{
cout<<temp.CalculateVolume()<<endl;
}
if(type==5)
{
Box NewBox(temp);
cout<<NewBox<<endl;
}
}
}
int main()
{
check2();
}
©️ Hackerrank. All Rights Reserved.
'프로그래밍 언어 > C, C++' 카테고리의 다른 글
[Hackerrank] 18. Exceptional Server (2) | 2024.02.14 |
---|---|
[Hackerrank] 17. Inherited Code (0) | 2024.02.14 |
[Hackerrank] 15. Classes and Objects (2) | 2024.02.09 |
[Hackerrank] 14. Classes (0) | 2024.02.09 |
[Hackerrank] 13. Structs (0) | 2024.02.09 |