📜  n 块的最小周长

📅  最后修改于: 2021-10-23 08:12:45             🧑  作者: Mango

给定 n 个大小为 1 x 1 的块,我们需要找到由这些块组成的网格的最小周长。
例子 :

Input : n = 4
Output : 8
Minimum possible perimeter with 4 blocks
is 8. See below explanation.

Input : n = 11
Output : 14
The square grid of above examples would be as

让我们举个例子来看看一个模式。假设我们有 4 个块,以下是不同的可能性

+--+--+--+--+
  |  |  |  |  |  Perimeter = 10
  +--+--+--+--+

  +--+--+--+
  |  |  |  |     Perimeter = 10
  +--+--+--+
        |  |
        +--+

  +--+--+--+
  |  |  |  |     Perimeter = 10
  +--+--+--+
     |  |
     +--+


  +--+--+
  |  |  |        Perimeter = 8
  +--+--+
  |  |  |
  +--+--+

如果我们用笔和纸做一些例子,我们可以注意到,当形成的形状最接近正方形时,周长变得最小。这样做的原因是,我们希望块的最大边面向形状内部,以便形状的周长变得最小。
如果块数是一个完美的正方形,那么周长就是 4*sqrt(n)。
但是,如果块数不是完美的平方根,那么我们计算最接近平方根的行数和列数。在将块排列成矩形后,我们仍然剩下块,然后我们将简单地将 2 添加到周长上,因为只剩下 2 个额外的边。
下面给出上述想法的实现。

C++
// CPP program to find minimum
// perimeter using n blocks.
#include 
using namespace std;
 
int minPerimeter(int n)
{
    int l = sqrt(n);
    int sq = l * l;
 
    // if n is a perfect square
    if (sq == n)
        return l * 4;
    else
    {
        // Number of rows
        long long int row = n / l;
 
        // perimeter of the
        // rectangular grid
        long long int perimeter
                      = 2 * (l + row);
 
        // if there are blocks left
        if (n % l != 0)
            perimeter += 2;
        return perimeter;
    }
}
 
// Driver code
int main()
{
    int n = 10;
    cout << minPerimeter(n);
    return 0;
}


Java
// JAVA Code to find minimum
// perimeter using n blocks
import java.util.*;
 
class GFG
{
    public static long minPerimeter(int n)
    {
        int l = (int) Math.sqrt(n);
        int sq = l * l;
     
        // if n is a perfect square
        if (sq == n)
            return l * 4;
        else
        {
            // Number of rows
            long row = n / l;
     
            // perimeter of the
            // rectangular grid
            long perimeter
                  = 2 * (l + row);
     
            // if there are blocks left
            if (n % l != 0)
                perimeter += 2;
            return perimeter;
        }
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 10;
        System.out.println(minPerimeter(n));
    }
}
 
// This code is contributed by Arnav Kr. Mandal


Python3
# Python3 program to find minimum
# perimeter using n blocks.
import math
 
def minPerimeter(n):
    l = math.sqrt(n)
    sq = l * l
  
    # if n is a perfect square
    if (sq == n):
        return l * 4
    else :
        # Number of rows
        row = n / l
  
        # perimeter of the
        # rectangular grid
        perimeter = 2 * (l + row)
                       
        # if there are blocks left
        if (n % l != 0):
            perimeter += 2
        return perimeter
 
# Driver code
n = 10
print(int(minPerimeter(n)))
 
# This code is contributed by
# Prasad Kshirsagar


C#
// C# Code to find minimum
// perimeter using n blocks
using System;
 
class GFG
{
    public static long minPerimeter(int n)
    {
        int l = (int) Math.Sqrt(n);
        int sq = l * l;
     
        // if n is a perfect square
        if (sq == n)
            return l * 4;
        else
        {
            // Number of rows
            long row = n / l;
         
            // perimeter of the
            // rectangular grid
            long perimeter
                  = 2 * (l + row);
     
            // if there are blocks left
            if (n % l != 0)
                perimeter += 2;
            return perimeter;
        }
    }
     
    // Driver code
    public static void Main()
    {
        int n = 10;
        Console.Write(minPerimeter(n));
    }
}
 
// This code is contributed by nitin mittal


PHP


Javascript


输出 :

14