📌  相关文章
📜  蛋糕数

📅  最后修改于: 2021-05-06 20:37:21             🧑  作者: Mango

考虑一个3D立方体和n个平面。我们可以使用给定的平面在立方体中进行切割。给定n的蛋糕数是n个平面可以形成的最大区域数。请参考此图以查看示例。

n个平面切割后的级数(0 <= n)
1,2,4,8,15,26,42,64,93,130,176,232,299,378,470,576,697………………………………。

例子 :

Input : 1
Output : 2
Explanation : 
With 1 plane cut the cube is divided into 2 regions

Input : 2
Output : 4
Explanation: 
With 2 plane cuts, we can divide the cube into 4 regions

Input : 4
Output : 15

Input : 5
Output : 26

建议:在继续解决方案之前,请先在{IDE}上尝试使用您的方法。

第n个蛋糕编号的公式:

下面是上述方法的实现:

C++
// CPP Program to find the
// nth Cake number
#include 
using namespace std;
  
// function for Cake number
int number_cake(int n)
{
    // formula for find  Cake number
    // nth term
    return (n * n * n + 5 * n + 6) / 6;
}
  
// Driver Code
int main()
{
    // For 2nd cake Number
    int n = 2;
    cout << number_cake(n) << endl;
  
    // For 8th cake Number
    n = 8;
    cout << number_cake(n) << endl;
    // For 25th cake Number
    n = 25;
    cout << number_cake(n) << endl;
  
    return 0;
}


Java
// Java Program to find the nth Cake number
import java.io.*;
  
class GFG {
      
    // function for Cake number
    static int number_cake(int n)
    {
          
        // formula for find Cake number
        // nth term
        return (n * n * n + 5 * n + 6) / 6;
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        // For 2nd cake Number
        int n = 2;
        System.out.println( number_cake(n));
      
        // For 8th cake Number
        n = 8;
        System.out.println( number_cake(n));
          
        // For 25th cake Number
        n = 25;
        System.out.println( number_cake(n));
    }
}
  
// This code is contributed by anuj_67.


Python3
# Python program to find
# nth Cake number
  
# Function to calculate
# Cake number
def number_cake(n):
  
    # Formula to calculate nth
    # Cake number
      
    return (n * n * n + 5 * n + 6) // 6
  
# Driver Code
n = 2
print(number_cake(n))
  
n = 8
print(number_cake(n))
  
n = 25
print(number_cake(n))
                      
# This code is contributed by aj_36


C#
// C# Program to find the nth Cake number
using System;
  
class GFG {
      
    // function for Cake number
    static int number_cake(int n)
    {
          
        // formula for find Cake number
        // nth term
        return (n * n * n + 5 * n + 6) / 6;
    }
      
    // Driver Code
    public static void Main ()
    {
        // For 2nd cake Number
        int n = 2;
        Console.WriteLine( number_cake(n));
      
        // For 8th cake Number
        n = 8;
        Console.WriteLine( number_cake(n));
          
        // For 25th cake Number
        n = 25;
        Console.WriteLine( number_cake(n));
    }
}
  
// This code is contributed by anuj_67.


PHP


输出 :

4
93
2626

参考资料: https : //en.wikipedia.org/wiki/Cake_number