码迷,mamicode.com
首页 > 其他好文 > 详细

已知矩形面积求最小周长

时间:2015-08-02 21:14:10      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

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. 
T10,n109
 

Output

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

Sample Input

3 2 7 12
 

Sample Output

6 16 14
 
题意:
已知矩形面积求最小周长;;;;
n*m=s;
2(n+m)=2(n+s/n);
当n最接近sqrt(s)时周长最小!!
技术分享
 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 int main(){
 5     int t;cin>>t;
 6     while(t--){
 7         int n;cin>>n;
 8         int m=floor(sqrt(n)+0.5);
 9         int sum=0;
10         for(int i=m;i>0;i--){
11             if(n%i==0){
12                 sum=max((i+n/i)*2,sum);
13                 break;
14             }
15         }
16         cout<<sum<<endl;
17     }
18 return 0;
19 }
View Code

 

已知矩形面积求最小周长

标签:

原文地址:http://www.cnblogs.com/demodemo/p/4696610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!