[Leetcode by Go 29]492. Construct the Rectangle

Source: Internet
Author: User
Tags square root
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:

    1. The area of the rectangular Web page is designed must equal to the given target area.
    2. The width W should not being larger than the length L, which means L >= W.
    3. 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:

    1. The given area won ' t exceed 10,000,000 and is a positive integer
    2. 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}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.