从给定数组打印下三角矩阵图案
给定一个数组,以 2D 形式打印数组,其中上三角形的值为 0,下三角形的值增加前缀大小(第一行的前缀大小为 1,第二行的前缀大小为 2,...)
例子 :
Input : 1 2 3 4 5
Output : 1 0 0 0 0
1 2 0 0 0
1 2 3 0 0
1 2 3 4 0
1 2 3 4 5
Input : 1 2 3
Output : 1 0 0
1 2 0
1 2 3
C++
// C++ implementation to print
// given array in 2D form
#include
using namespace std;
// Function to print pattern
void printPattern(int arr[], int n)
{
// array to store the final output
int b[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
// if i > j then insert arr[j]
// in 2D array b[i][j]
if (i >= j)
b[i][j] = arr[j];
else
// insert zero in array b[i][j]
b[i][j] = 0;
}
// printing pattern in 2-D array i.e,
// printing 2D array b[i][j]
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << b[i][j] << " ";
cout << endl;
}
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
printPattern(arr, n);
return 0;
}
Java
// Java implementation to print
// given array in 2D form
import java.io.*;
class GFG
{
// Function to print pattern
static void printPattern(int arr[], int n)
{
// array to store the final output
int b[][] = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
// if i > j then insert arr[j]
// in 2D array b[i][j]
if (i >= j)
b[i][j] = arr[j];
else
// insert zero in array b[i][j]
b[i][j] = 0;
}
// printing pattern in 2-D array i.e,
// printing 2D array b[i][j]
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
System.out.print(b[i][j] +" ");
System.out.println();
}
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = arr.length;
printPattern(arr, n);
}
}
// This code is contributed by vt_m.
Python3
# Python3 implementation to print
# given array in 2D form
# Function to print pattern
def printPattern(arr, n):
# array to store the final output
b = [[0 for i in range(n)]
for i in range(n)]
for i in range(0, n):
for j in range(0, n):
# if i > j then insert arr[j]
# in 2D array b[i][j]
if (i >= j):
b[i][j] = arr[j];
else:
# insert zero in array b[i][j]
b[i][j] = 0;
# printing pattern in 2-D array i.e,
# printing 2D array b[i][j]
for i in range(0, n):
for j in range(0, n):
print(b[i][j], end = " ");
print();
# Driver code
arr = [ 1, 2, 3, 4, 5 ];
n = len(arr);
printPattern(arr, n);
# This code is contributed by
# Shivi_Aggarwal
C#
// C# implementation to print
// given array in 2D form
using System;
namespace Array
{
public class GFG
{
// Function to print pattern
static void printPattern(int []arr, int n)
{
// array to store the final output
int[,] b = new int[n,n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
// if i > j then insert arr[j]
// in 2D array b[i][j]
if (i >= j)
b[i,j] = arr[j];
else
// insert zero in array b[i][j]
b[i,j] = 0;
}
// printing pattern in 2-D array i.e,
// printing 2D array b[i][j]
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write(b[i,j] + " ");
Console.WriteLine();
}
}
// Driver code
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
printPattern(arr, n);
}
}
}
// This code is contributed by Sam007
PHP
$j then insert $arr[$j]
// in 2D $array $b[$i][$j]
if ($i >= $j)
$b[$i][$j] = $arr[$j];
else
// insert zero in
// $array $b[$i][$j]
$b[$i][$j] = 0;
}
// printing pattern in 2-D
// $array i.e, printing 2D
// $array $b[$i][$j]
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
echo $b[$i][$j]." ";
echo "\n";
}
}
// Driver code
$arr = array(1, 2, 3, 4, 5);
$n = sizeof($arr) / sizeof($arr[0]);
printPattern($arr, $n);
// This code is contributed by Mithun Kumar
?>
Javascript
输出:
1 0 0 0 0
1 2 0 0 0
1 2 3 0 0
1 2 3 4 0
1 2 3 4 5