描述
给出两个长方形的长和宽(长不一定大于宽),实现长方形类的一个方法,判定前者是否能完全覆盖后者。
输入描述:
输入4个整数,前两个表示第一个长方形的长和宽,后两个表示第二个长方形的长和宽。
输出描述:
如果前者能完全覆盖后者输出"yes"否则输出"no"
cpp
#include<bits/stdc++.h>
using namespace std;
class rectangle{
private:
int length,width;
public:
void set(int x,int y){
length=x;
width=y;
}
int getlength(){
return length;
}
int getwidth(){
return width;
}
int area(){
return length*width;
}
int find_max(){
return (length > width)?length:width;
}
int find_min(){
return (length < width)?length:width;
}
// int find_max()
// {
// if(length >width)
// return length;
// else
// return width;
// }
// int find_min()
// {
// if(length < width)
// return length;
// else
// return width;
// }
string cancover(rectangle b)
{
if((this->find_max() >= b.find_max()) &&(this->find_min() >= b.find_min()))
{
return "yes";
}else {
return "no";
}
}
};
int main(){
int l1,w1,l2,w2;
cin>>l1>>w1>>l2>>w2;
rectangle a,b;
a.set(l1,w1);
b.set(l2,w2);
cout<<a.cancover(b);
return 0;
}
cpp
#include <iostream>
#include <algorithm>//用max、min函数
using namespace std;
//长方形类
class Rectangle{
private:
int length;
int width;
public:
//设置长宽
void setValue(int x,int y)
{
length = x;
width = y;
}
//判断当前长方形能否覆盖另一个长方形
bool cover(Rectangle r)
{
//把自己的长边、短边算出来
// my_max、my_min 就是 this->max、this->min
// 也就是:当前调用这个方法的长方形自己!
int my_max = max(length,width);
int my_min = min(length,width);
//把对方的长边、短边算出来
int r_max = max(r.length,r.width);
int r_min = min(r.length,r.width);
// 自己的大边 ≥ 对方大边 并且 自己的小边 ≥ 对方小边
if (my_max >= r_max && my_min >= r_min) {
return true; // 能覆盖
} else {
return false; // 不能覆盖
}
}
};
int main() {
int l1, w1, l2, w2;
cin >> l1 >> w1 >> l2 >> w2;
Rectangle r1, r2;
r1.set(l1, w1);
r2.set(l2, w2);
//this 就是 r1,r 就是 r2
if (r1.cover(r2)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
return 0;
}