📜  检查是否可以从N * M的面积中获得面积P

📅  最后修改于: 2021-04-17 17:12:17             🧑  作者: Mango

给定整数NMP ,任务是检查是否有可能在N×M sq单位的矩形区域内找到P sq单位的矩形区域。

例子:

方法:按照以下步骤解决问题

  • 找到所有p因子,并将它们存储在向量中,例如factor
  • 保持阶数N≤M
  • 遍历矢量因子
    • 对于每个数组元素factor [i] ,检查是否factor [i]≤Np / factor [i]≤M ,其中factor [i]p / factor [i]表示矩形区域的尺寸。
    • 如果发现为真,则打印“是”并返回。
  • 否则,打印NO

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if a rectangle
// of p sq units can be formed from
//  an area of n * m sq units
void splitArea(int n, int m, int p)
{
    // Maintain order n <= m
    if (n > m)
        swap(n, m);
 
    vector factors;
 
    // Iterate to find factors of p
    for (int i = 1; i * i <= p; i++) {
 
        // p is divisible by i
        if (p % i == 0) {
            factors.push_back(i);
        }
    }
 
    for (int i = 0; i < (int)factors.size();
         i++) {
 
        // Check if dimensions
        // lie in given area
        if (factors[i] <= n &&
            p / factors[i] <= m) {
 
            cout << "YES";
            return;
        }
    }
 
    cout << "NO";
}
 
// Driver Code
int main()
{
    int n = 3, m = 3, p = 4;
    splitArea(n, m, p);
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to swap two numbers
  // Using temporary variable
  static void swap(int m, int n)
  {
     
    // Swapping the values
    int temp = m;
    m = n;
    n = temp;
  }
 
  // Function to check if a rectangle
  // of p sq units can be formed from
  //  an area of n * m sq units
  static void splitArea(int n, int m, int p)
  {
 
    // Maintain order n <= m
    if (n > m)
      swap(n, m);
    ArrayList factors = new ArrayList();
 
    // Iterate to find factors of p
    for (int i = 1; i * i <= p; i++)
    {
 
      // p is divisible by i
      if (p % i == 0)
      {
        factors.add(i);
      }
    }
 
    for (int i = 0; i < (int)factors.size();
         i++)
    {
 
      // Check if dimensions
      // lie in given area
      if (factors.get(i) <= n &&
          p / factors.get(i) <= m)
      {
        System.out.print("YES");
        return;
      }
    }
 
    System.out.print("NO");
  }
 
 
// Driver code
public static void main(String[] args)
{
    int n = 3, m = 3, p = 4;
    splitArea(n, m, p);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to check if a rectangle
# of p sq units can be formed from
# an area of n * m sq units
def splitArea(n, m, p):
   
    # Maintain order n <= m
    if (n > m):
        n, m = m, n
    factors = []
 
    # Iterate to find factors of p
    for i in range(1, p + 1):
 
        # p is divisible by i
        if (p % i == 0):
            factors.append(i)
 
    for i in range(len(factors)):
       
        # Check if dimensions
        # lie in given area
        if (factors[i] <= n and p // factors[i] <= m):
            print("YES")
            return
 
    print("NO")
 
# Driver Code
if __name__ == '__main__':
    n, m, p = 3, 3, 4
    splitArea(n, m, p)
 
    # This code is contributed by mohit kumar 29.


C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
   
  // Function to swap two numbers
  // Using temporary variable
  static void swap(int m, int n)
  {
     
    // Swapping the values
    int temp = m;
    m = n;
    n = temp;
  }
 
  // Function to check if a rectangle
  // of p sq units can be formed from
  //  an area of n * m sq units
  static void splitArea(int n, int m, int p)
  {
 
    // Maintain order n <= m
    if (n > m)
      swap(n, m);
    List factors = new List();
 
    // Iterate to find factors of p
    for (int i = 1; i * i <= p; i++)
    {
 
      // p is divisible by i
      if (p % i == 0)
      {
        factors.Add(i);
      }
    }
 
    for (int i = 0; i < (int)factors.Count;
         i++)
    {
 
      // Check if dimensions
      // lie in given area
      if (factors[i] <= n &&
          p / factors[i] <= m)
      {
        Console.Write("YES");
        return;
      }
    }
 
    Console.Write("NO");
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int n = 3, m = 3, p = 4;
    splitArea(n, m, p);
  }
}
 
// This code is contributed by splevel62.


输出:
YES

时间复杂度: O(√P)
辅助空间: O(log(P))