# 算法设计：最小正方形可均匀切割矩形

2021年3月19日12:43:10 发表评论 836 次浏览

## C ++

``````// CPP program to find minimum number of
// squares to make a given rectangle.
#include <bits/stdc++.h>
using namespace std;

int countRectangles( int l, int w)
{
// if we take gcd(l, w), this
// will be largest possible
// side for suare, hence minimum
// number of square.
int squareSide = __gcd(l, w);

// Number of squares.
return (l * w) / (squareSide * squareSide);
}

// Driver code
int main()
{
int l = 4, w = 6;
cout << countRectangles(l, w) << endl;
return 0;
}``````

## Java

``````// Java program to find minimum number of
// squares to make a given rectangle.

class GFG{
static int __gcd( int a, int b) {
if (b== 0 ) return a;
return __gcd(b, a%b);
}
static int countRectangles( int l, int w)
{
// if we take gcd(l, w), this
// will be largest possible
// side for suare, hence minimum
// number of square.
int squareSide = __gcd(l, w);

// Number of squares.
return (l * w) / (squareSide * squareSide);
}

// Driver code
public static void main(String[] args)
{
int l = 4 , w = 6 ;
System.out.println(countRectangles(l, w));
}
}
// This code is contributed by mits``````

## Python3

``````# Python3 code to find minimum number of
# squares to make a given rectangle.

import math

def countRectangles(l, w):

# if we take gcd(l, w), this
# will be largest possible
# side for suare, hence minimum
# number of square.
squareSide = math.gcd(l, w)

# Number of squares.
return (l * w) / (squareSide * squareSide)

# Driver Code

if __name__ = = '__main__' :
l = 4
w = 6
ans = countRectangles(l, w)
print ( int (ans))

# this code is contributed by
# SURENDRA_GANGWAR``````

## C#

``````// C# program to find minimum number of
// squares to make a given rectangle.

class GFG{
static int __gcd( int a, int b) {
if (b==0) return a;
return __gcd(b, a%b);
}
static int countRectangles( int l, int w)
{
// if we take gcd(l, w), this
// will be largest possible
// side for suare, hence minimum
// number of square.
int squareSide = __gcd(l, w);

// Number of squares.
return (l * w) / (squareSide * squareSide);
}

// Driver code
public static void Main()
{
int l = 4, w = 6;
System.Console.WriteLine(countRectangles(l, w));
}
}
// This code is contributed by mits``````

## 的PHP

``````<?php
// PHP program to find minimum number
// of squares to make a given rectangle.

function gcd( \$a , \$b )
{
return \$b ? gcd( \$b , \$a % \$b ) : \$a ;
}

function countRectangles( \$l , \$w )
{
// if we take gcd(l, w), this
// will be largest possible
// side for suare, hence minimum
// number of square.
\$squareSide = gcd( \$l , \$w );

// Number of squares.
return ( \$l * \$w ) / ( \$squareSide *
\$squareSide );
}

// Driver code
\$l = 4;
\$w = 6;
echo countRectangles( \$l , \$w ) . "\n" ;

// This code is contributed
// by ChitraNayal
?>``````

``6``