📜  在二维矩阵中打印同心矩形图案

📅  最后修改于: 2022-05-13 01:57:23.936000             🧑  作者: Mango

在二维矩阵中打印同心矩形图案

给定一个正整数n ,打印填充有矩形图案的矩阵,如下所示:
啊啊啊啊
阿布阿
abcb
阿布阿
啊啊啊啊
其中 a = n, b = n – 1, c = n – 2 等等。
例子:

Input : n = 4
Output :
4 4 4 4 4 4 4 
4 3 3 3 3 3 4 
4 3 2 2 2 3 4 
4 3 2 1 2 3 4 
4 3 2 2 2 3 4 
4 3 3 3 3 3 4 
4 4 4 4 4 4 4 

Input : n = 3
Output :
3 3 3 3 3 
3 2 2 2 3 
3 2 1 2 3 
3 2 2 2 3 
3 3 3 3 3 

对于给定的n ,要打印的行数或列数将为 2*n – 1。我们将分两部分打印矩阵。我们将首先打印从 0 到 floor((2*n – 1)/2) 的行的上半部分,然后从 floor((2*n – 1)/2) + 1 到 2*n – 2 打印下半部分。
现在对于每一行,我们将把它分成三部分打印。第一部分是递减序列,从 n 开始,每次迭代递减 1。迭代次数将等于行号,第二部分是一个常数序列,其中常数为 n – i 将打印 2*n – 1 – 2 * 行号,第三部分是递增序列,什么都没有但与第一个序列相反。
对于下半部分,观察,它是上半部分的镜像(不包括中间行)。因此,只需运行从 (2*n – 1)/2 到 0 的上半部分循环。
下面是这种方法的基本实现:

C++
// C++ program for printing
// the rectangular pattern
#include 
using namespace std;
 
// Function to print the pattern
void printPattern(int n)
{
    // number of rows and columns to be printed
    int s = 2 * n - 1;
 
    // Upper Half
    for (int i = 0; i < (s / 2) + 1; i++) {
        
        int m = n;
 
        // Decreasing part
        for (int j = 0; j < i; j++) {
            cout << m << " ";
            m--;
        }
 
        // Conatsant Part
        for (int k = 0; k < s - 2 * i; k++) {
            cout << n - i << " ";
        }
 
        // Increasing part.
        m = n - i + 1;
        for (int l = 0; l < i; l++) {
            cout << m << " ";
            m++;
        }
 
        cout << endl;
    }
 
    // Lower Half
    for (int i = s / 2 - 1; i >= 0; i--) {
 
        // Decreasing Part
        int m = n;
        for (int j = 0; j < i; j++) {
            cout << m << " ";
            m--;
        }
 
        // Constant Part.
        for (int k = 0; k < s - 2 * i; k++) {
            cout << n - i << " ";
        }
 
        // Decreasing Part
        m = n - i + 1;
        for (int l = 0; l < i; l++) {
            cout << m << " ";
            m++;
        }
        cout << endl;
    }
}
// Driven Program
int main()
{
    int n = 3;
 
    printPattern(n);
    return 0;
}


Java
// Java program for printing
// the rectangular pattern
import java.io.*;
 
class GFG
{
 
// Function to
// print the pattern
static void printPattern(int n)
{
    // number of rows and
    // columns to be printed
    int s = 2 * n - 1;
 
    // Upper Half
    for (int i = 0;
             i < (s / 2) + 1; i++)
    {
        int m = n;
 
        // Decreasing part
        for (int j = 0; j < i; j++)
        {
            System.out.print(m + " ");
            m--;
        }
 
        // Constant Part
        for (int k = 0;
                 k < s - 2 * i; k++)
        {
            System.out.print(n - i + " ");
        }
 
        // Increasing part.
        m = n - i + 1;
        for (int l = 0; l < i; l++)
        {
            System.out.print(m + " ");
            m++;
        }
 
        System.out.println();
    }
 
    // Lower Half
    for (int i = s / 2 - 1;
             i >= 0; i--)
    {
 
        // Decreasing Part
        int m = n;
        for (int j = 0; j < i; j++)
        {
            System.out.print(m + " ");
            m--;
        }
 
        // Constant Part.
        for (int k = 0;
                 k < s - 2 * i; k++)
        {
            System.out.print(n - i + " ");
        }
 
        // Decreasing Part
        m = n - i + 1;
        for (int l = 0; l < i; l++)
        {
            System.out.print(m + " ");
            m++;
        }
        System.out.println();
    }
}
// Driver Code
public static void main (String[] args)
{
    int n = 3;
 
    printPattern(n);
}
}
 
// This code is contributed
// by anuj_67.


Python3
# Python3 program for printing
# the rectangular pattern
 
# Function to print the pattern
def printPattern(n) :
 
    # number of rows and columns to be printed
    s = 2 * n - 1
 
    # Upper Half
    for i in range(0, int(s / 2) + 1):
         
        m = n
 
        # Decreasing part
        for j in range(0, i):
            print(m ,end= " " )
            m-=1
         
        # Conatsant Part
        for k in range(0, s - 2 * i):
            print(n-i ,end= " " )
         
        # Increasing part.
        m = n - i + 1
        for l in range(0, i):
            print(m ,end= " " )
            m+=1
         
        print("")
     
    # Lower Half
    for i in range(int(s / 2),-1,-1):
 
        # Decreasing Part
        m = n
        for j in range(0, i):
            print(m ,end= " " )
            m-=1
         
 
        # Constant Part.
        for k in range(0, s - 2 * i):
            print(n-i ,end= " " )
         
 
        # Decreasing Part
        m = n - i + 1
        for l in range(0, i):
            print(m ,end= " " )
            m+=1
         
        print("")
     
# Driven Program
if __name__=='__main__':
    n = 3
    printPattern(n)
 
# this code is contributed by Smitha Dinesh
# Semwal


C#
// C# program for printing
// the rectangular pattern
using System;
 
class GFG
{
 
// Function to
// print the pattern
static void printPattern(int n)
{
    // number of rows and
    // columns to be printed
    int s = 2 * n - 1;
 
    // Upper Half
    for (int i = 0;
             i < (s / 2) + 1; i++)
    {
        int m = n;
 
        // Decreasing part
        for (int j = 0; j < i; j++)
        {
            Console.Write(m + " ");
            m--;
        }
 
        // Constant Part
        for (int k = 0;
                 k < s - 2 * i; k++)
        {
            Console.Write(n - i + " ");
        }
 
        // Increasing part.
        m = n - i + 1;
        for (int l = 0; l < i; l++)
        {
            Console.Write(m + " ");
            m++;
        }
 
        Console.WriteLine();
    }
 
    // Lower Half
    for (int i = s / 2 - 1;
             i >= 0; i--)
    {
 
        // Decreasing Part
        int m = n;
        for (int j = 0; j < i; j++)
        {
            Console.Write(m + " ");
            m--;
        }
 
        // Constant Part.
        for (int k = 0;
                 k < s - 2 * i; k++)
        {
            Console.Write(n - i + " ");
        }
 
        // Decreasing Part
        m = n - i + 1;
        for (int l = 0; l < i; l++)
        {
            Console.Write(m + " ");
            m++;
        }
        Console.WriteLine();
    }
}
// Driver Code
public static void Main ()
{
    int n = 3;
 
    printPattern(n);
}
}
 
// This code is contributed
// by anuj_67.


PHP
= 0; $i--)
    {
 
        // Decreasing Part
        $m = $n;
        for ($j = 0; $j < $i; $j++)
        {
            echo $m, " ";
            $m--;
        }
 
        // Constant Part.
        for ($k = 0; $k < $s - 2 * $i; $k++)
        {
            echo $n - $i , " ";
        }
 
        // Decreasing Part
        $m = $n - $i + 1;
        for ($l = 0; $l < $i; $l++)
        {
            echo $m , " ";
            $m++;
        }
        echo "\n";
    }
}
 
// Driver Code
$n = 3;
printPattern($n);
 
// This code is contributed
// by Sach_Code   
?>


Javascript


C++
// C++ program for printing
// the rectangular pattern
#include
using namespace std;
 
// Function to print the pattern
void printPattern(int n)
{
    int arraySize = n * 2 - 1;
    int result[arraySize][arraySize];
         
    // Fill the values
    for(int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < arraySize; j++)
        {
            if(abs(i - arraySize / 2) > abs(j - arraySize / 2))
                result[i][j] = abs(i - arraySize / 2) + 1;
            else
                result[i][j] = (abs(j-arraySize / 2) + 1);
             
        }
    }
         
    // Print the array
    for(int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < arraySize; j++)
        {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int n = 3;
 
    printPattern(n);
    return 0;
}
 
// This code is contributed
// by Rajput-Ji.


Java
// Java program for printing
// the rectangular pattern
import java.io.*;
 
class GFG
{
 
// Function to print the pattern
static void printPattern(int n)
{
        int arraySize = n * 2 - 1;
        int[][] result = new int[arraySize][arraySize];
         
        //Fill the values
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
                result[i][j] = Math.max(Math.abs(i-arraySize/2),
                                    Math.abs(j-arraySize/2))+1;
            }
        }
         
        //Print the array
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
            System.out.print(result[i][j]);
            }
            System.out.println();
        }
}
// Driver Code
public static void main (String[] args)
{
    int n = 3;
 
    printPattern(n);
}
}
 
// This code is contributed
// by MohitSharma23.


Python3
# Python3 program for printing
# the rectangular pattern
 
# Function to print the pattern
def printPattern(n):
 
    arraySize = n * 2 - 1;
    result = [[0 for x in range(arraySize)]
                 for y in range(arraySize)];
         
    # Fill the values
    for i in range(arraySize):
        for j in range(arraySize):
            if(abs(i - (arraySize // 2)) >
               abs(j - (arraySize // 2))):
                result[i][j] = abs(i - (arraySize // 2)) + 1;
            else:
                result[i][j] = abs(j - (arraySize // 2)) + 1;
             
    # Print the array
    for i in range(arraySize):
        for j in range(arraySize):
            print(result[i][j], end = " ");
        print("");
 
# Driver Code
n = 3;
 
printPattern(n);
 
# This code is contributed by mits


C#
// C# program for printing
// the rectangular pattern
using System;
 
class GFG
{
 
// Function to print the pattern
static void printPattern(int n)
{
        int arraySize = n * 2 - 1;
        int[,] result = new int[arraySize,arraySize];
         
        // Fill the values
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
                result[i,j] = Math.Max(Math.Abs(i-arraySize/2),
                                    Math.Abs(j-arraySize/2))+1;
            }
        }
         
        // Print the array
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
                Console.Write(result[i,j]+" ");
            }
            Console.WriteLine();
        }
}
 
// Driver Code
public static void Main (String[] args)
{
    int n = 3;
 
    printPattern(n);
}
}
 
// This code has been contributed by 29AjayKumar


PHP

                    abs($j - (int)($arraySize / 2)))
                $result[$i][$j] = abs($i -
                        (int)($arraySize / 2)) + 1;
            else
                $result[$i][$j] = (abs($j-(int)
                                ($arraySize / 2)) + 1);
             
        }
    }
         
    // Print the array
    for($i = 0; $i < $arraySize; $i++)
    {
        for($j = 0; $j < $arraySize; $j++)
        {
            echo $result[$i][$j]." ";
        }
        echo "\n";
    }
}
 
// Driver Code
 
    $n = 3;
 
    printPattern($n);
 
// This code is contributed by mits
?>


Javascript


输出

3 3 3 3 3 
3 2 2 2 3 
3 2 1 2 3 
3 2 2 2 3 
3 3 3 3 3  

另一种方法:



C++

// C++ program for printing
// the rectangular pattern
#include
using namespace std;
 
// Function to print the pattern
void printPattern(int n)
{
    int arraySize = n * 2 - 1;
    int result[arraySize][arraySize];
         
    // Fill the values
    for(int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < arraySize; j++)
        {
            if(abs(i - arraySize / 2) > abs(j - arraySize / 2))
                result[i][j] = abs(i - arraySize / 2) + 1;
            else
                result[i][j] = (abs(j-arraySize / 2) + 1);
             
        }
    }
         
    // Print the array
    for(int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < arraySize; j++)
        {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int n = 3;
 
    printPattern(n);
    return 0;
}
 
// This code is contributed
// by Rajput-Ji.

Java

// Java program for printing
// the rectangular pattern
import java.io.*;
 
class GFG
{
 
// Function to print the pattern
static void printPattern(int n)
{
        int arraySize = n * 2 - 1;
        int[][] result = new int[arraySize][arraySize];
         
        //Fill the values
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
                result[i][j] = Math.max(Math.abs(i-arraySize/2),
                                    Math.abs(j-arraySize/2))+1;
            }
        }
         
        //Print the array
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
            System.out.print(result[i][j]);
            }
            System.out.println();
        }
}
// Driver Code
public static void main (String[] args)
{
    int n = 3;
 
    printPattern(n);
}
}
 
// This code is contributed
// by MohitSharma23.

蟒蛇3

# Python3 program for printing
# the rectangular pattern
 
# Function to print the pattern
def printPattern(n):
 
    arraySize = n * 2 - 1;
    result = [[0 for x in range(arraySize)]
                 for y in range(arraySize)];
         
    # Fill the values
    for i in range(arraySize):
        for j in range(arraySize):
            if(abs(i - (arraySize // 2)) >
               abs(j - (arraySize // 2))):
                result[i][j] = abs(i - (arraySize // 2)) + 1;
            else:
                result[i][j] = abs(j - (arraySize // 2)) + 1;
             
    # Print the array
    for i in range(arraySize):
        for j in range(arraySize):
            print(result[i][j], end = " ");
        print("");
 
# Driver Code
n = 3;
 
printPattern(n);
 
# This code is contributed by mits

C#

// C# program for printing
// the rectangular pattern
using System;
 
class GFG
{
 
// Function to print the pattern
static void printPattern(int n)
{
        int arraySize = n * 2 - 1;
        int[,] result = new int[arraySize,arraySize];
         
        // Fill the values
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
                result[i,j] = Math.Max(Math.Abs(i-arraySize/2),
                                    Math.Abs(j-arraySize/2))+1;
            }
        }
         
        // Print the array
        for(int i = 0; i < arraySize; i++)
        {
            for(int j = 0; j < arraySize; j++)
            {
                Console.Write(result[i,j]+" ");
            }
            Console.WriteLine();
        }
}
 
// Driver Code
public static void Main (String[] args)
{
    int n = 3;
 
    printPattern(n);
}
}
 
// This code has been contributed by 29AjayKumar

PHP


                    abs($j - (int)($arraySize / 2)))
                $result[$i][$j] = abs($i -
                        (int)($arraySize / 2)) + 1;
            else
                $result[$i][$j] = (abs($j-(int)
                                ($arraySize / 2)) + 1);
             
        }
    }
         
    // Print the array
    for($i = 0; $i < $arraySize; $i++)
    {
        for($j = 0; $j < $arraySize; $j++)
        {
            echo $result[$i][$j]." ";
        }
        echo "\n";
    }
}
 
// Driver Code
 
    $n = 3;
 
    printPattern($n);
 
// This code is contributed by mits
?>

Javascript


输出

3 3 3 3 3 
3 2 2 2 3 
3 2 1 2 3 
3 2 2 2 3 
3 3 3 3 3  

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程