📜  修改的Pascal三角形中给定级别上存在的所有数字的总和

📅  最后修改于: 2021-04-29 10:35:49             🧑  作者: Mango

给定级别N,任务是找到在给定级别上出现的所有交替Pascal三角形中的整数之和。
修改后的Pascal三角形有5个等级,如下所示。

1
   -1 1
   1 -2 1
 -1 3 -3 1
1 -4 6 -4 1

例子:

Input: N = 1
Output: 1

Input: N = 2
Output: 0

方法:正如我们可以看到的,对于偶数级,和为0,对于奇数级,除了1和也为0。因此最多可以有2种情况:

  • 如果L = 1,则答案为1。
  • 否则,答案将始终为0。

下面是上述方法的实现:

C++
// C++ program to calculate sum of
// all the numbers present at given
// level in an Modified Pascal’s triangle
 
#include 
using namespace std;
 
// Function to calculate sum
void ans(int n)
{
    if (n == 1)
        cout << "1";
    else
        cout << "0";
}
 
// Driver Code
int main()
{
    int n = 2;
    ans(n);
 
    return 0;
}


Java
// Java program to calculate sum of
// all the numbers present at given
// level in an Modified Pascal's triangle
class GFG
{
 
// Function to calculate sum
static void ans(int n)
{
    if (n == 1)
        System.out.println("1");
    else
        System.out.println("0");
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 2;
    ans(n);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to calculate sum of
# all the numbers present at given
# level in an Modified Pascal’s triangle
 
# Function to calculate sum
def ans(n) :
 
    if (n == 1) :
        print("1",end="");
    else :
        print("0",end="");
 
# Driver Code
if __name__ == "__main__" :
 
    n = 2;
    ans(n);
     
# This code is contributed by AnkitRai01


C#
// C# program to calculate sum of
// all the numbers present at given
// level in an Modified Pascal's triangle
using System;
     
class GFG
{
 
// Function to calculate sum
static void ans(int n)
{
    if (n == 1)
        Console.WriteLine("1");
    else
        Console.WriteLine("0");
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 2;
    ans(n);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
0

时间复杂度: O(1)