题目:
For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
-
The area of the rectangular web page you designed must equal to the given target area.
-
The width W should not be larger than the length L, which means L >= W.
-
The difference between length L and width W should be as small as possible.
注意:
- 他说You need to output the length L and the width W of the web page you designed in sequence.
- 思路是(int)平方,然后遍历把符合条件的一遍遍替代存进去,最后一个肯定是相差最小的。
**Code:
public int[] constructRectangle(int area) {
int [] res = new int[2];
int l = 0;
int w = 0;
for(int i=1;i<=(int)Math.sqrt(area);i++){
if(area%i==0){
w= i;
l = area/i;
}
res[0] = l;
res[1] = w;
}
return res;
}