为给定数组打印 Sum Triangle 的程序
给定一个数组,编写一个程序来构造一个三角形,其中最后一行包含给定数组的元素,倒数第二行的每个元素都包含以下两个元素的和,依此类推。
例子:
Input: arr[] = {4, 7, 3, 6, 7};
Output:
81
40 41
21 19 22
11 10 9 13
4 7 3 6 7
Input: {10, 40, 50}
Output:
140
50 90
10 40 50
关于输出的一个重要观察是最终值在顶部,顶部元素需要首先打印。因此,我们使用二维辅助数组以自底向上的方式构造三角形,然后打印三角形。二维数组的元素 tri[i][j] 可以计算为 tri[i+1][j] 和 tri[i+1][j+1] 的和。
以下是上述想法的实现:
C++
// C++ program to print sum triangle for a given array
#include
using namespace std;
// prints sum triangle for arr[0..n-1]
void printTriangle(int arr[], int n)
{
// Initialize a 2D array to store triangle
int tri[n][n];
memset(tri, 0, sizeof(tri));
// Initialize last row of triangle
for (int i = 0; i < n ; i++)
tri[n-1][i] = arr[i];
// Fill other rows
for (int i = n-2; i >=0; i--)
for (int j = 0; j <= i; j++)
tri[i][j] = tri[i+1][j] + tri[i+1][j+1];
// Print the triangle
for (int i = 0; i < n; i++)
{
for(int j = 0; j <= i ; j++)
cout << tri[i][j]<<" ";
cout << endl;
}
}
// Driver Program
int main()
{
int arr[] = {4, 7, 3, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
printTriangle(arr, n);
return 0;
}
Java
// Java program to print sum triangle for a given array
class Test{
static int arr[] = new int[]{4, 7, 3, 6, 7};
// prints sum triangle for arr[0..n-1]
public static void printTriangle(int n)
{
// Initialize a 2D array to store triangle
int tri[][] = new int[n][n];
// Initialize last row of triangle
for (int i = 0; i < n ; i++)
tri[n-1][i] = arr[i];
// Fill other rows
for (int i = n-2; i >=0; i--)
for (int j = 0; j <= i; j++)
tri[i][j] = tri[i+1][j] + tri[i+1][j+1];
// Print the triangle
for (int i = 0; i < n; i++)
{
for(int j = 0; j <= i ; j++)
System.out.print(tri[i][j] + " ");
System.out.println();
}
}
public static void main(String[] args)
{
printTriangle(arr.length);
}
}
Python3
# Python 3 program to print sum triangle
# for a given array
# prints sum triangle for arr[0..n-1]
def printTriangle(arr, n):
# Initialize a 2D array to store triangle
tri = [[0 for i in range(n)]
for i in range(n)]
# Initialize last row of triangle
for i in range(n):
tri[n - 1][i] = arr[i]
# Fill other rows
i = n - 2
while(i >= 0):
for j in range(0, i + 1, 1):
tri[i][j] = (tri[i + 1][j] +
tri[i + 1][j + 1])
i -= 1
# Print the triangle
for i in range(0, n, 1):
for j in range(0, i + 1, 1):
print(tri[i][j], end = " ")
print("\n", end = "")
# Driver Code
if __name__ == '__main__':
arr = [4, 7, 3, 6, 7]
n = len(arr)
printTriangle(arr, n)
# This code is contributed by
# Shashank_Sharma
C#
// C# program to print sum triangle
// for a given array
using System;
class GFG {
static int []arr = new int[]{4, 7, 3, 6, 7};
// prints sum triangle for arr[0..n-1]
public static void printTriangle(int n)
{
// Initialize a 2D array to store triangle
int [,]tri = new int[n, n];
// Initialize last row of triangle
for (int i = 0; i < n ; i++)
tri[n - 1, i] = arr[i];
// Fill other rows
for (int i = n - 2; i >= 0; i--)
for (int j = 0; j <= i; j++)
tri[i, j] = tri[i + 1, j] +
tri[i + 1, j + 1];
// Print the triangle
for (int i = 0; i < n; i++)
{
for(int j = 0; j <= i ; j++)
Console.Write(tri[i, j] + " ");
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
printTriangle(arr.Length);
}
}
// This code is contributed by Sam007.
PHP
= 0; $i--)
for ($j = 0; $j <= $i; $j++)
$tri[$i][$j] = $tri[$i + 1][$j] +
$tri[$i + 1][$j + 1];
// Print the triangle
for ($i = 0; $i < $n; $i++)
{
for( $j = 0; $j <= $i ; $j++)
echo $tri[$i][$j] . " ";
echo "\n";
}
}
// Driver Code
$arr = array(4, 7, 3, 6, 7);
$n = count($arr);
printTriangle($arr, $n);
// This code is contributed by Rajput-Ji
?>
Javascript
输出:
81
40 41
21 19 22
11 10 9 13
4 7 3 6 7