492.Construct the Rectangle
示例:
输入: 4
输出: [2, 2]
解释: 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。
但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。
说明:
给定的面积不大于 10,000,000 且为正整数。
你设计的页面的长度和宽度必须都是正整数。class Solution {
public:
vector<int> constructRectangle(int area) {
int s=sqrt(area);
// cout<<"s "<<s<<endl;
int q=0;
for(int i=s;i>=1;--i)
{
if(area%i ==0) {q=i; break;}
}
if(!q)q=1;
vector<int >res(2);
res[1]=q;
res[0]=area/q;
return res;
}
};Last updated