📜  剪纸成最小平方数

📅  最后修改于: 2021-06-25 14:14:43             🧑  作者: 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号的Paper剪切至少9个正方形。 x 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