This is a creation in Article, where the information may have evolved or changed.
Immediately wrote 30 questions, using Golang to write the code concise and clear, but also very convenient to write test cases, plus goland can be debugged, like God help.
But anyway, write the test will rely on the test to judge right and wrong, with the debug will rely on debug to find the place of error, these are actually their brains lazy put pressure on the test and tools, in the daily development can improve the code quality and efficiency, But in the written interview, the compiler does not debug the code, let alone write test cases.
Therefore, if you can solve the problem directly, you do not write test cases, I also save (write) the ().
Topic
For a web developer, it's very important to know what to design a Web page ' s size. So, given a specific rectangular Web page's area, your job by now-to design a rectangular web page, whose length L and Width W satisfy the following requirements:
- The area of the rectangular Web page is designed must equal to the given target area.
- The width W should not being 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.
Example:
Input: 4
Output: [2, 2]
explanation: The target area was 4, and all of the possible ways to construct it was [1,4], [2,2], [4,1].
But according to Requirement 2, [1,4] is illegal; According to Requirement 3, [4,1] isn't optimal compared to [2,2]. The length L is 2, and the width W are 2.
Note:
- The given area won ' t exceed 10,000,000 and is a positive integer
- The Web page ' s width and length you designed must is positive integers.
Thinking of solving problems
The square root of the area, then the root as the width W, if areas can be divisible by W, then l=area/w, otherwise w minus 1, then the same judgment
Attention
Some people think that the root can be judged as a decreasing width, or as a length increment. But considering the possibility of getting a decimal after the square root, if the root is converted to int, l < (AREA/L) does not match the actual, so the square root should be the width
Code
Constructrectangle.go
package _492_Construct_the_Rectangleimport "math"func constructRectangle(area int) []int { var ret []int sqrtArea := math.Sqrt(float64(area)) var w int w = int(sqrtArea) for ; w > 0; w-- { if area % w == 0 { l := area / w ret = []int{l, w} break } } return ret}