蛋糕号

考虑3D立方体和N平面。我们可以用给定的平面在立方体上切割。给定n的饼数是n个平面可以形成的最大区域数。 n个平面切割后的系列(0≤ n) : 1, 2, 4, 8, 15, 26, 42, 64, 93, 130, 176, 232, 299, 378, 470, 576, 697…

null

例如:

Input : 1Output : 2Explanation : With 1 plane cut the cube is divided into 2 regionsInput : 2Output : 4Explanation: With 2 plane cuts, we can divide the cube into 4 regionsInput : 4Output : 15Input : 5Output : 26

饼数n项公式:

第n个蛋糕编号= N C 3. + N C 2. + N C 1. + N C 0 =(n) 3. +5*n+6)/6

以下是上述方法的实施情况:

C++

// CPP Program to find the
// nth Cake number
#include <iostream>
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

<?php
// PHP Program to find the
// nth Cake number
// function for Cake number
function number_cake( $n )
{
// formula for find Cake
// number nth term
return ( $n * $n * $n +
5 * $n + 6) / 6;
}
// Driver Code
// For 2nd cake Number
$n = 2;
echo number_cake( $n ) , "" ;
// For 8th cake Number
$n = 8;
echo number_cake( $n ), " " ;
// For 25th cake Number
$n = 25;
echo number_cake( $n );
// This code is contributed by anuj_67.
?>


Javascript

<script>
// Javascript Program to find the nth Cake number
// function for Cake number
function number_cake(n)
{
// formula for find Cake number
// nth term
return parseInt((n * n * n + 5 * n + 6) / 6, 10);
}
// For 2nd cake Number
let n = 2;
document.write( number_cake(n) + "</br>" );
// For 8th cake Number
n = 8;
document.write( number_cake(n) + "</br>" );
// For 25th cake Number
n = 25;
document.write( number_cake(n));
</script>


输出:

4932626

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

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享