Problem:
给定一个矩形的面积,求这个矩形的长和宽,使得长宽都是整数且差最小。
Solution:
从根号下面积开始逐渐递减,直到找到可整除的数。
//Solution1:
class Solution {
public:
vector<int> constructRectangle(int area) {
int m = sqrt(area);
vector<int> ans;
while(m > 0) {
if(area%m == 0) {
ans.push_back(area/m);
ans.push_back(m);
break;
}
m--;
}
return ans;
}
};