📜  阵列中的三角形总和

📅  最后修改于: 2021-04-27 19:56:37             🧑  作者: Mango

给定一个整数数组,从中打印一个总和三角形,以使第一级具有所有数组元素。从那时起,每个级别上的元素数量比上一个级别少一个,并且该级别上的元素是上一个级别中连续两个元素的总和。
例子 :

Input : A = {1, 2, 3, 4, 5}
Output : [48]
         [20, 28] 
         [8, 12, 16] 
         [3, 5, 7, 9] 
         [1, 2, 3, 4, 5] 

Explanation :
Here,   [48]
        [20, 28] -->(20 + 28 = 48)
        [8, 12, 16] -->(8 + 12 = 20, 12 + 16 = 28)
        [3, 5, 7, 9] -->(3 + 5 = 8, 5 + 7 = 12, 7 + 9 = 16)
        [1, 2, 3, 4, 5] -->(1 + 2 = 3, 2 + 3 = 5, 3 + 4 = 7, 4 + 5 = 9)

方法 :

  1. 递归是关键。在每次迭代时,创建一个新数组,其中包含该数组遍历中连续元素的总和作为参数。
  2. 进行递归调用,并在上一步中传递新创建的数组。
  3. 反向跟踪时,打印阵列(以相反顺序打印)。

    下面是上述方法的实现:

    C++
    // C++ program to create Special triangle.
    #include
    using namespace std;
      
    // Function to generate Special Triangle
    void printTriangle(int A[] , int n)
        {
            // Base case
            if (n < 1)
                return;
      
            // Creating new array which contains the
            // Sum of consecutive elements in
            // the array passes as parameter.
            int temp[n - 1];
            for (int i = 0; i < n - 1; i++)
            {
                int x = A[i] + A[i + 1];
                temp[i] = x;
            }
      
            // Make a recursive call and pass
            // the newly created array
            printTriangle(temp, n - 1);
      
            // Print current array in the end so
            // that smaller arrays are printed first
            for (int i = 0; i < n ; i++)
            {
                if(i == n - 1)
                    cout << A[i] << " ";
                else
                cout << A[i] << ", ";
            }
                      
            cout << endl;
        }
      
        // Driver function
        int main()
        {
            int A[] = { 1, 2, 3, 4, 5 };
            int n = sizeof(A) / sizeof(A[0]);
              
            printTriangle(A, n);
        }
          
    // This code is contributed by Smitha Dinesh Semwal


    Java
    // Java program to create Special triangle.
    import java.util.*;
    import java.lang.*;
      
    public class ConstructTriangle
    {
        // Function to generate Special Triangle.
        public static void printTriangle(int[] A)
        {
            // Base case
            if (A.length < 1)
                return;
      
            // Creating new array which contains the
            // Sum of consecutive elements in
            // the array passes as parameter.
            int[] temp = new int[A.length - 1];
            for (int i = 0; i < A.length - 1; i++)
            {
                int x = A[i] + A[i + 1];
                temp[i] = x;
            }
      
            // Make a recursive call and pass
            // the newly created array
            printTriangle(temp);
      
            // Print current array in the end so
            // that smaller arrays are printed first
            System.out.println(Arrays.toString(A));
        }
      
        // Driver function
        public static void main(String[] args)
        {
            int[] A = { 1, 2, 3, 4, 5 };
            printTriangle(A);
        }
    }


    Python3
    # Python3 program to create Special triangle.
    # Function to generate Special Triangle.
    def printTriangle(A):
              
            # Base case
            if (len(A) < 1):
                return
      
            # Creating new array which contains the
            # Sum of consecutive elements in
            # the array passes as parameter.
            temp = [0] * (len(A) - 1)
            for i in range( 0, len(A) - 1):
              
                x = A[i] + A[i + 1]
                temp[i] = x
              
      
            # Make a recursive call and pass
            # the newly created array
            printTriangle(temp)
              
            # Print current array in the end so
            # that smaller arrays are printed first
            print(A)
          
      
    # Driver function
    A = [ 1, 2, 3, 4, 5 ]
    printTriangle(A)
      
    # This code is contributed by Smitha Dinesh Semwal


    C#
    // C# program to create Special triangle.
       
    using System;
                          
    public class ConstructTriangle
    {
    // Function to generate Special Triangle
    static void printTriangle(int []A, int n)
        {
            // Base case
            if (n < 1)
                return;
       
            // Creating new array which contains the
            // Sum of consecutive elements in
            // the array passes as parameter.
            int []temp = new int[n - 1];
            for (int i = 0; i < n - 1; i++)
            {
                int x = A[i] + A[i + 1];
                temp[i] = x;
            }
       
            // Make a recursive call and pass
            // the newly created array
            printTriangle(temp, n - 1);
       
            // Print current array in the end so
            // that smaller arrays are printed first
            for (int i = 0; i < n ; i++)
            {
                if(i == n - 1)
                    Console.Write(A[i] + " ");
                else
                Console.Write(A[i] + ", ");
            }
                       
            Console.WriteLine();
        }
       
        // Driver function
        public static void Main()
        {
            int[] A = { 1, 2, 3, 4, 5 };
            int n = A.Length;
            printTriangle(A,n);
        }
    }
      
    //This code contributed by 29AjayKumar


    PHP


    输出 :

    [48]
    [20, 28]
    [8, 12, 16]
    [3, 5, 7, 9]
    [1, 2, 3, 4, 5]