To find the perimeter by area ...

Source: Internet
Author: User

Description

There is a piece of paper in front of Tom, it length and width are integers. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper.

Input

In the first line, there are an integer T indicates the number of test cases. In the next T-lines, there is only one of the integer-n-Every line, indicates the area of paper.$T \leq 10,n\leq {10}^{9}$

Output

For each case, output a integer, indicates the answer.

Sample Input

3 2 7 12

Sample Output

6 16 14 Topic meaning: Tell you a rectangular area to ask for his minimum perimeter problem solving ideas: the most straightforward way to think of a method is to enumerate and then compare, unfortunately, this will time out ... Then there is a more accelerated approach. Consider that the smaller the difference between x and Y, the smaller the perimeter. So you can save time by opening squares. First, the area open square, after the turn into shaping, and then calculate whether the area can be divided into the number of the total, can not be reduced by one, until the completion of the division ..... This will find an edge of the minimum perimeter, and then you can output the minimum perimeter. Why is it that one side of the smallest perimeter is found?     It can be likened, when you open the square is the number of the midpoint, and then left to go, found the first to meet the division is the shortest distance between x and y a pair first to a timeout code bar. (Good contrast)
1#include <stdio.h>2 3 intMain ()4 {5     intT;6scanf"%d",&T);7      while(t--)8     {9         ints,sum=10000, k=0;Tenscanf"%d",&S); One          for(intx=1; x<=s; X + +) A         { -             if(s%x==0&&sum>= (x+s/x)) -sum=x+s/x; the         } -printf"%d\n",2*sum); -     } -     return 0; +}

Next is the code that does not time out:

1#include <cstdio>2#include <cmath>3 intMain ()4 {5     intT,s,n;6scanf"%d",&t);7      while(t--)8     {9scanf"%d",&s);TenN= (int) sqrt (Double) s); One          while(s%N) A            { -                //printf ("%d\n", n); -n--; the            } -n=2* (n+s/n); -printf"%d\n", n); -     } + return 0; -}

To find the perimeter by area ...

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.