zoukankan      html  css  js  c++  java
  • 51nod 最小周长

    题目来源: 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
    输出最小周长。

    一开始直接遍历超时,由数学定理,周长一定正方形面积最大,则面积一定正方形周长最小,从int(sqrt(周长))向左右遍历可以提高效率
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    #define MAXN 1001
    #define INF 0x3f3f3f3f
    LL solve(LL n)
    {
        LL m = INF,t;
        LL a = (LL)sqrt((double)n);
        LL d = a*a>n? -1:1;
        t=a;
        while(n%t)
        {
            t+=d;
        }
        return (t+n/t)*2;
    }
    int main()
    {
        LL n;
        cin>>n;
        cout<<solve(n)<<endl;
        return 0;
    }
  • 相关阅读:
    枚举
    枚举
    比特币中的密码学原理
    贪心
    dp
    二分
    mac解决matplotlib中文乱码
    Keras使用多个GPU并行
    pyspark使用-dataframe操作
    箱线图
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6258931.html
Copyright © 2011-2022 走看看