原文:

给定一个正整数 n ,打印填充矩形图案的矩阵如下图: t3【a a a a a a a at5a b b b b at8a b c b at11a b b b b at14a a a at17】其中 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 到楼层((2 * n–1)/2)的行打印上半部分,然后从楼层((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--;
        }
        // constant 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.

python 3

# 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
        # constant 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.

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 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   
?>

java 描述语言


输出

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.

python 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

服务器端编程语言(professional hypertext preprocessor 的缩写)


                    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
?>

java 描述语言


输出

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