c.Tom and paper

Tom and paper

Description

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

Input

In the first line, there is an integer T indicates the number of test cases. In the next T lines, there is only one integer n in every line, indicates the area of paper.            
      
                

Output

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

Sample Input

3
2
7
12
                

Sample Output

6
16
14
 
题目大意:
已知面积,求最小周长。
 
分析:
要想求最小周长要已知一条边。C=2*(a/i+i)或C=2*(1+n)将两个进行比较,输出最小值。
注意:
判断i的范围,想到正方形边长=sqrt(n)
 
代码:
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int T;
10     scanf("%d",&T);
11     while(T--)
12     {
13         int n;
14         scanf("%d",&n);
15         int a,c;        
16         for(int i=1;i<=sqrt(n);i++)
17         {
18             a=2*(n+1);
19             
20             if(n%i==0)
21                 c=2*(n/i+i);
22                 if(c<=a)
23                     a=c;
24         }
25         printf("%d\n",a);
26     }
27     return 0;
28 }
View Code
原文地址:https://www.cnblogs.com/ttmj865/p/4693233.html