zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Go
    Go
    Go -11 Go 框架beego的简单 create run
    文本处理工具之:grep sed awk
    Linux系统布置java项目
    docker 启动mysql 本地连接
    time
    多行查询数据合并成一行进行展示
    settings的使用
    xlsxwriter
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4693002.html
Copyright © 2011-2022 走看看