📜  剪成最少正方形的纸

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

给定一张大小为 A x B 的纸。任务是将纸剪成任意大小的正方形。找出可以从纸上剪下的最少正方形数。
例子:

Input  : 13 x 29
Output : 9
Explanation : 
2 (squares of size 13x13) + 
4 (squares of size 3x3) + 
3 (squares of size 1x1)=9

Input  : 4 x 5
Output : 5
Explanation : 
1 (squares of size 4x4) + 
4 (squares of size 1x1)

我们知道,如果我们想从纸上切出最少数量的正方形,那么我们必须首先从纸上切出最大的正方形,并且最大的正方形将与纸的较小边具有相同的边。例如,如果纸张的尺寸为 13 x 29,那么最大的正方形将是边 13。因此我们可以切割 2 个尺寸为 13 x 13 (29/13 = 2) 的正方形。现在剩余的纸将有 3 x 13 的尺寸。类似地,我们可以使用 4 个尺寸为 3 x 3 的正方形和 3 个 1 x 1 的正方形切割剩余的纸。因此,从尺寸为 13 的纸上至少可以切割 9 个正方形× 29。

挖1

下面是上述方法的实现。

C++
// C++ program to find minimum number of squares
// to cut a paper.
#include
using namespace std;
 
// Returns min number of squares needed
int minimumSquare(int a, int b)
{
    long long result = 0, rem = 0;
 
    // swap if a is small size side .
    if (a < b)
        swap(a, b);
 
    // Iterate until small size side is
    // greater then 0
    while (b > 0)
    {
        // Update result
        result += a/b;
 
        long long rem = a % b;
        a = b;
        b = rem;
    }
 
    return result;
}
 
// Driver code
int main()
{
    int n = 13, m = 29;
    cout << minimumSquare(n, m);
    return 0;
}


Java
// Java program to find minimum
// number of squares to cut a paper.
class GFG{
     
// To swap two numbers
static void swap(int a,int b)
{
    int temp = a;
    a = b;
    b = temp;
}
 
// Returns min number of squares needed
static int minimumSquare(int a, int b)
{
    int result = 0, rem = 0;
 
    // swap if a is small size side .
    if (a < b)
        swap(a, b);
 
    // Iterate until small size side is
    // greater then 0
    while (b > 0)
    {
        // Update result
        result += a/b;
        rem = a % b;
        a = b;
        b = rem;
    }
 
    return result;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 13, m = 29;
    System.out.println(minimumSquare(n, m));
}
}
 
//This code is contributed by Smitha Dinesh Semwal.


Python3
# Python 3 program to find minimum
# number of squares to cut a paper.
 
# Returns min number of squares needed
def minimumSquare(a, b):
 
    result = 0
    rem = 0
 
    # swap if a is small size side .
    if (a < b):
        a, b = b, a
 
    # Iterate until small size side is
    # greater then 0
    while (b > 0):
     
        # Update result
        result += int(a / b)
 
        rem = int(a % b)
        a = b
        b = rem
 
    return result
 
# Driver code
n = 13
m = 29
 
print(minimumSquare(n, m))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to find minimum
// number of squares to cut a paper.
using System;
     
class GFG
{
     
// To swap two numbers
static void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}
 
// Returns min number of squares needed
static int minimumSquare(int a, int b)
{
    int result = 0, rem = 0;
 
    // swap if a is small size side .
    if (a < b)
        swap(a, b);
 
    // Iterate until small size side is
    // greater then 0
    while (b > 0)
    {
        // Update result
        result += a / b;
        rem = a % b;
        a = b;
        b = rem;
    }
    return result;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 13, m = 29;
    Console.WriteLine(minimumSquare(n, m));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

9

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程