📜  计算大小为 N 的立方体中内接的大小为 K 的立方体

📅  最后修改于: 2021-10-23 09:12:07             🧑  作者: Mango

给定两个整数NK ,任务是找到大小为 N 的立方体中可以包含的大小为K的立方体的数量。

例子:

方法:解决问题的关键观察是大小为N的立方体内部的立方体数量为(N 2 * (N+1) 2 )/4 。因此,大小为 N 的立方体中大小为K的立方体为:

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
// Function to find the number
// of the cubes of the size K
int No_of_cubes(int N, int K)
{
    int No = 0;
 
    // Stores the number of cubes
    No = (N - K + 1);
 
    // Stores the number of cubes
    // of size k
    No = pow(No, 3);
    return No;
}
 
// Driver Code
int main()
{
    // Size of the bigger cube
    int N = 5;
 
    // Size of the smaller cube
    int K = 2;
 
    cout << No_of_cubes(N, K);
    return 0;
}


Java
// Java implementation of the
// above approach
class GFG{
 
// Function to find the number
// of the cubes of the size K
static int No_of_cubes(int N,
                       int K)
{
  int No = 0;
 
  // Stores the number of cubes
  No = (N - K + 1);
 
  // Stores the number of cubes
  // of size k
  No = (int) Math.pow(No, 3);
  return No;
}
 
// Driver Code
public static void main(String[] args)
{
  // Size of the bigger cube
  int N = 5;
 
  // Size of the smaller cube
  int K = 2;
 
  System.out.print(No_of_cubes(N, K));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the
# above approach
  
# Function to find the number
# of the cubes of the size K
def No_of_cubes(N, K):
     
    No = 0
  
    # Stores the number of cubes
    No = (N - K + 1)
  
    # Stores the number of cubes
    # of size k
    No = pow(No, 3)
    return No
 
# Driver Code
 
# Size of the bigger cube
N = 5
  
# Size of the smaller cube
K = 2
  
print(No_of_cubes(N, K))
 
# This code is contributed by sanjoy_62


C#
// C# implementation of the
// above approach
using System;
  
class GFG{
      
// Function to find the number
// of the cubes of the size K
static int No_of_cubes(int N, int K)
{
    int No = 0;
     
    // Stores the number of cubes
    No = (N - K + 1);
     
    // Stores the number of cubes
    // of size k
    No = (int)Math.Pow(No, 3);
    return No;
}
  
// Driver Code
public static void Main()
{
     
    // Size of the bigger cube
    int N = 5;
     
    // Size of the smaller cube
    int K = 2;
     
    Console.Write(No_of_cubes(N, K));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
64

时间复杂度: O(1)
辅助空间: O(1)

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