51nod 1283 最小周长【注意开根号】

题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
一个矩形的面积为S,已知该矩形的边长都是整数,求所有满足条件的矩形中,周长的最小值。例如:S = 24,那么有{1 24} {2 12} {3 8} {4 6}这4种矩形,其中{4 6}的周长最小,为20。
Input
输入1个数S(1 <= S <= 10^9)。
Output
输出最小周长。
Input示例
24
Output示例
20

【分析】:注意开平方根(否则超时),类比查找质数。
【代码】:
#include <bits/stdc++.h>

using namespace std;
#define LL long long
LL n;
int main()
{
   while(~scanf("%lld",&n))
   {
       LL minn=999999999;
       for(int i=1;i<=sqrt(n);i++)
       {
           if(n%i==0)
            minn=min(minn,i+n/i);
       }
       printf("%lld\n",minn*2);
   }
    return 0;
}
View Code

 

posted @ 2017-10-31 19:59  Roni_i  阅读(178)  评论(0编辑  收藏  举报