📜  计数刻在大小为N的立方体中的大小为K的立方体

📅  最后修改于: 2021-04-21 21:37:17             🧑  作者: 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)