Número máximo de piezas en N cortes

Dado un trozo cuadrado y un número total de cortes disponibles n, averigüe el número máximo de piezas rectangulares o cuadradas de igual tamaño que se pueden obtener con n cortes. Los cortes permitidos son corte horizontal y vertical. 
Nota: No se permite apilar ni plegar.
Ejemplos
 

Input : n = 1
Output : 2
Explanation : 

Input : n = 2
Output : 4
Explanation : 

Input : n = 3
Output : 6
Explanation : 

 

Dado es n que es el número de cortes permitidos. Como se requiere maximizar el número de piezas después de n cortes, el número de cortes horizontales será igual al número de cortes verticales. Esto se puede probar usando la diferenciación. Entonces el número de cortes horizontales será n/2. y los cortes verticales serán nn/2.
Entonces número de piezas = (corte horizontal + 1) * (corte vertical + 1).
Programa: 
 

C++

// C++ program to find maximum no of pieces
// by given number of cuts
#include <bits/stdc++.h>
using namespace std;
 
// Function for finding maximum pieces
// with n cuts.
int findMaximumPieces(int n)
{
    // to maximize number of pieces
    // x is the horizontal cuts
    int x = n / 2;
 
    // Now (x) is the horizontal cuts
    // and (n-x) is vertical cuts, then
    // maximum number of pieces = (x+1)*(n-x+1)
    return ((x + 1) * (n - x + 1));
}
 
// Driver code
int main()
{
 
    // Taking the maximum number of cuts allowed as 3
    int n = 3;
 
    // Finding and printing the max number of pieces
    cout << "Max number of pieces for n = " << n
         << " is " << findMaximumPieces(3);
 
    return 0;
}

Java

// Java program to find maximum
// no of pieces by given number
// of cuts
import java.util.*;
 
class GFG
{
// Function for finding maximum
// pieces with n cuts.
public static int findMaximumPieces(int n)
{
    // to maximize number of pieces
    // x is the horizontal cuts
    int x = n / 2;
 
    // Now (x) is the horizontal cuts
    // and (n-x) is vertical cuts, then
    // maximum number of pieces = (x+1)*(n-x+1)
    return ((x + 1) * (n - x + 1));
}
 
// Driver code
public static void main (String[] args)
{
    // Taking the maximum number
    // of cuts allowed as 3
    int n = 3;
     
    // Finding and printing the
    // max number of pieces
    System.out.print("Max number of pieces for n = " +
                   n + " is " + findMaximumPieces(3));
         
}
}
 
// This code is contributed by Kirti_Mangal

Python 3

# Python 3 program to find maximum no of pieces
# by given number of cuts
  
# Function for finding maximum pieces
# with n cuts.
def findMaximumPieces(n):
 
    # to maximize number of pieces
    # x is the horizontal cuts
    x = n // 2
  
    # Now (x) is the horizontal cuts
    # and (n-x) is vertical cuts, then
    # maximum number of pieces = (x+1)*(n-x+1)
    return ((x + 1) * (n - x + 1))
  
# Driver code
if __name__ == "__main__":
  
    #Taking the maximum number of cuts allowed as 3
    n = 3
  
    # Finding and printing the max number of pieces
    print("Max number of pieces for n = " +str( n)
         +" is " + str(findMaximumPieces(3)))
 
# This code is contributed by ChitraNayal

C#

// C# program to find maximum
// no of pieces by given number
// of cuts
using System;
 
class GFG
{
 
// Function for finding maximum
// pieces with n cuts.
public static int findMaximumPieces(int n)
{
    // to maximize number of pieces
    // x is the horizontal cuts
    int x = n / 2;
 
    // Now (x) is the horizontal 
    // cuts and (n-x) is vertical
    // cuts, then maximum number
    // of pieces = (x+1)*(n-x+1)
    return ((x + 1) * (n - x + 1));
}
 
// Driver code
static public void Main ()
{
    // Taking the maximum number
    // of cuts allowed as 3
    int n = 3;
     
    // Finding and printing the
    // max number of pieces
    Console.Write("Max number of pieces for n = " +
                n + " is " + findMaximumPieces(3));
}
}
 
// This code is contributed by Mahadev

PHP

<?php
// PHP program to find maximum no
// of pieces by given number of cuts
 
// Function for finding maximum
// pieces with n cuts.
function findMaximumPieces($n)
{
    // to maximize number of pieces
    // x is the horizontal cuts
    $x = (int)($n / 2);
 
    // Now (x) is the horizontal cuts
    // and (n-x) is vertical cuts, then
    // maximum number of pieces = (x+1)*(n-x+1)
    return (($x + 1) * ($n - $x + 1));
}
 
// Driver code
 
// Taking the maximum number
// of cuts allowed as 3
$n = 3;
 
// Finding and printing the
// max number of pieces
echo "Max number of pieces for n = " .
    $n . " is " . findMaximumPieces(3);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>

Javascript

<script>
 
// Javascript program to find maximum no of pieces
// by given number of cuts
 
// Function for finding maximum pieces
// with n cuts.
function findMaximumPieces(n)
{
 
    // to maximize number of pieces
    // x is the horizontal cuts
    var x = parseInt(n / 2);
 
    // Now (x) is the horizontal cuts
    // and (n-x) is vertical cuts, then
    // maximum number of pieces = (x+1)*(n-x+1)
    return ((x + 1) * (n - x + 1));
}
 
// Driver code
// Taking the maximum number of cuts allowed as 3
var n = 3;
 
// Finding and printing the max number of pieces
document.write("Max number of pieces for n = " + n
    + " is " + findMaximumPieces(3));
 
// This code is contributed by noob2000.
</script>
Producción: 

Max number of pieces for n = 3 is 6

 

Tiempo Complejidad: O(1) 
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *