Подсчитайте кубики размера K, вписанные в куб размера N
Для двух целых чисел N и K задача состоит в том, чтобы найти количество кубиков размера K, которые могут содержаться в кубе размера N.
Примеры:
Input: N = 2, K = 1
Output: 8
Explanation:
There are 8 cubes of size 1 that can be drawn inside the bigger cube of size 2.
Input: N = 5, K = 2
Output: 64
Explanation:
There are 64 cubes of size 2 can be drawn inside the bigger cube of size 5.
Подход: ключевое наблюдение для решения проблемы состоит в том, что количество кубиков внутри куба размера N равно (N 2 * (N + 1) 2 ) / 4 . Следовательно, кубы размера K внутри куба размера N:
Ниже представлена реализация описанного выше подхода:
C ++
// C++ implementation of the// above approach#include <bits/stdc++.h>using namespace std;// Function to find the number// of the cubes of the size Kint 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); No; return}// Driver Codeint 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 implementation of the// above approachclass GFG{// Function to find the number// of the cubes of the size Kstatic 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 ); No; return}// Driver Codepublic 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 Kdef 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 cubeN = 5 # Size of the smaller cubeK = 2 print (No_of_cubes(N, K))# This code is contributed by sanjoy_62 |
C #
// C# implementation of the// above approachusing System; class GFG{ // Function to find the number// of the cubes of the size Kstatic 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); No; return} // Driver Codepublic 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
<script>// JavaScript program for// the above approach// Function to find the number// of the cubes of the size Kfunction No_of_cubes(N, K){ let No = 0; // Stores the number of cubes No = (N - K + 1); // Stores the number of cubes // of size k No = Math.pow(No, 3); No; return}// Driver code // Size of the bigger cube let N = 5; // Size of the smaller cube let K = 2; document.write(No_of_cubes(N, K)); // This code is contributed by splevel62.</script> |
64
Сложность времени: O (1)
Вспомогательное пространство: O (1)
Вниманию читателя! Не прекращайте учиться сейчас. Получите все важные математические концепции для соревновательного программирования с курсом Essential Maths for CP по доступной для студентов цене. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .

