Title Link: Tom and paper
Surface:
Tom and Paper Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 679 Accepted Submission (s): 432
Problem 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≤10,n≤109
Output for each case, output a integer, indicates the answer.
Sample Input
3 2 7 12
Sample Output
6 16 14
Source Bestcoder Round #40
Solving:
If the circumference is fixed, take the value near the half value, the area is the largest. Conversely, take the square root near the longest perimeter.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int t,n,root;
cin>>t;
while (t--)
{
cin>>n;
ROOT=SQRT (1.0*n);
for (int i=root;; i--)
{
if (n%i==0)
{
cout<<2* (i+n/i) <<endl;
Break
;
}}} return 0;
}