2015 HUAS Summer Contest#2~C

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. 
$Tleq 10,nleq {10}^{9}$
 

Output

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

Sample Input

3
2
7
12
 

Sample Output

6
16
14
解题思路:这个问题首先要注意的事是时间限制问题,如果直接按部就班写出代码会超时,所以可以技巧的运用,当它为正方形时周长可以达到最小,所以限制条件可以改成面积的平方根。
程序代码:
#include<cstdio>
#include<cmath>
int main()
{
	
	int T,i,x,y,area,min,mini;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&area);
		min=2*1+2*area;
		mini=min;
		for(i=2;i<=sqrt(area);i++)
		{
			if(area%i==0)
			{
				x=i;
				y=area/i;
				min=2*x+2*y;
					if(min>mini)
						min=mini;
					else
						mini=min;
			}
		}
		printf("%d
",min);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/chenchunhui/p/4693002.html