使用立方体形成的长度为 H 的金字塔中未着色细胞的计数
给定一个金字塔,用单位面积的立方体形成,给定高度H。然后将金字塔放在地上并从外面涂漆。任务是找出未着色的立方体的数量。
例子:
Input: H = 3
Output: 1
Explanation:
Input: H = 1
Output: 0
朴素方法:保持未着色的立方体的数量是金字塔边界中不存在的立方体的数量。因此,对于高度为h的每一层立方体,无色立方体的数量为其中h>1 。因此,在每个级别添加无色立方体的数量以获得答案。
下面是上述方法的实现:
C++
// C++ program for the above problem
#include
using namespace std;
// Function to return the number of
// uncoloured cubes
int uncolouredCubes(int H)
{
// To store the number of uncoloured
// cubes
int res = 0;
for (int h = 2; h <= H; h++) {
res += (h - 2) * (h - 2);
}
return res;
}
// Driver Code
int main()
{
int H = 3;
cout << uncolouredCubes(H);
}
Java
// Java program for the above problem
class GFG{
// Function to return the number of
// uncoloured cubes
static int uncolouredCubes(int H)
{
// To store the number of uncoloured
// cubes
int res = 0;
for (int h = 2; h <= H; h++) {
res += (h - 2) * (h - 2);
}
return res;
}
// Driver Code
public static void main(String [] args)
{
int H = 3;
System.out.println(uncolouredCubes(H));
}
}
// This code is contributed by ihritik
Python3
# python program for the above problem
# Function to return the number of
# uncoloured cubes
def uncolouredCubes(H):
# To store the number of uncoloured
# cubes
res = 0
for h in range(2, H + 1):
res = res + (h - 2) * (h - 2)
return res
# Driver Code
H = 3
print(uncolouredCubes(H))
# This code is contributed by ihritik
C#
// C# program for the above problem
using System;
class GFG{
// Function to return the number of
// uncoloured cubes
static int uncolouredCubes(int H)
{
// To store the number of uncoloured
// cubes
int res = 0;
for (int h = 2; h <= H; h++) {
res += (h - 2) * (h - 2);
}
return res;
}
// Driver Code
public static void Main()
{
int H = 3;
Console.WriteLine(uncolouredCubes(H));
}
}
// This code is contributed by ihritik
Javascript
C++
// C++ program for the above problem
#include
using namespace std;
// Function to return the number of
// uncoloured cubes
int uncolouredCubes(int H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
int n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
int main()
{
int H = 3;
cout << uncolouredCubes(H);
}
Java
// Java program for the above problem
import java.util.*;
public class GFG
{
// Function to return the number of
// uncoloured cubes
static int uncolouredCubes(int H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
int n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
public static void main(String args[])
{
int H = 3;
System.out.println(uncolouredCubes(H));
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python program for the above problem
# Function to return the number of
# uncoloured cubes
def uncolouredCubes(H):
# If H is less than 2, then
# returning 0
if (H < 2):
return 0;
n = H - 2;
# Sum of the squares of first
# n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
# Driver Code
H = 3;
print((int)(uncolouredCubes(H)));
# This code is contributed by Saurabh Jaiswal.
C#
// C# program for the above problem
using System;
class GFG
{
// Function to return the number of
// uncoloured cubes
static int uncolouredCubes(int H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
int n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
public static void Main()
{
int H = 3;
Console.Write(uncolouredCubes(H));
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
// Javascript program for the above problem
// Function to return the number of
// uncoloured cubes
function uncolouredCubes(H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
let n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
let H = 3;
document.write(uncolouredCubes(H));
// This code is contributed by gfgking.
输出
1
时间复杂度: O(N)
辅助空间: O(1)
有效方法:由于每层中在高度h处的无色立方体的数量为 ,所以这个问题可以简化为找到第一个(H-2)个自然数的平方和。所以,答案是(n * (n + 1) * (2n + 1)) / 6其中n=H-2 。
C++
// C++ program for the above problem
#include
using namespace std;
// Function to return the number of
// uncoloured cubes
int uncolouredCubes(int H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
int n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
int main()
{
int H = 3;
cout << uncolouredCubes(H);
}
Java
// Java program for the above problem
import java.util.*;
public class GFG
{
// Function to return the number of
// uncoloured cubes
static int uncolouredCubes(int H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
int n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
public static void main(String args[])
{
int H = 3;
System.out.println(uncolouredCubes(H));
}
}
// This code is contributed by Samim Hossain Mondal
Python3
# Python program for the above problem
# Function to return the number of
# uncoloured cubes
def uncolouredCubes(H):
# If H is less than 2, then
# returning 0
if (H < 2):
return 0;
n = H - 2;
# Sum of the squares of first
# n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
# Driver Code
H = 3;
print((int)(uncolouredCubes(H)));
# This code is contributed by Saurabh Jaiswal.
C#
// C# program for the above problem
using System;
class GFG
{
// Function to return the number of
// uncoloured cubes
static int uncolouredCubes(int H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
int n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
public static void Main()
{
int H = 3;
Console.Write(uncolouredCubes(H));
}
}
// This code is contributed by Samim Hossain Mondal
Javascript
// Javascript program for the above problem
// Function to return the number of
// uncoloured cubes
function uncolouredCubes(H)
{
// If H is less than 2, then
// returning 0
if (H < 2) {
return 0;
}
let n = H - 2;
// Sum of the squares of first
// n natural numbers
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Driver Code
let H = 3;
document.write(uncolouredCubes(H));
// This code is contributed by gfgking.
输出
1
时间复杂度: O(1)
辅助空间: O(1)