原文:

给定一个方阵 mat[][] 和一个正整数 k 。任务是打印mat【】【】k 边界。

示例:

输入: mat[][] = {{1,2,3,4,5},k = 1 {6,7,8,9,10} {11,12,13,14,15} {16,17,18,19,20} {21,22,23,24,25}} 输出:1 2 3 4 5 6 10【t100

输入: mat[][] = {{1,2,3},k = 2 {4,5,6} {7,8,9}} 输出: 5

方法:这个问题是基于实现的。遍历矩阵,检查每个元素是否位于 kth 边界上。如果是,则打印元素否则打印空格字符。按照以下步骤解决给定的问题。

  • 对于0n
    • 用于从 0n 的 j
      • if((i = = k–1 或 i = = n–k)和(j > = k–1 和 j < = n–k))
        • 打印垫[i][j]
      • 否则,如果(j = = k–1 或 j = = n–k)和(i > = k–1 和 i < = n–k):
        • 打印垫[i][j]
  • 这将给出 mat[][]所需的 kth 边框

下面是上述方法的实现。

c

// c   program to implement
// the above approach
#include 
using namespace std;
// function to print kth border of a matrix
void printkthborder(vector> mat, int n, int k)
{
    for (int i = 0; i < n; i  )
    {
        cout << endl;
        for (int j = 0; j < n; j  )
        {
            // to keep track of which element to skip
            int flag = 0;
            if ((i == k - 1 || i == n - k) &&
                (j >= k - 1 && j <= n - k)) {
                // print the element
                cout << mat[i][j] << " ";
                flag = 1;
            }
            else if ((j == k - 1 || j == n - k) &&
                    (i >= k - 1 && i <= n - k)) {
                // print the element
                cout << mat[i][j] << " ";
                flag = 1;
            }
            if (flag == 0)
                cout << "  ";
        }
    }
}
// driver code
int main() {
    int n = 5;
    int k = 1;
    vector> mat = {{1, 2, 3, 4, 5},
                            {6, 7, 8, 9, 10},
                            {11, 12, 13, 14, 15},
                            {16, 17, 18, 19, 20},
                            {21, 22, 23, 24, 25}};
    printkthborder(mat, n, k);
}
// this code is contributed by samim hossain mondal.

java 语言(一种计算机语言,尤用于创建网站)

// java program to implement
// the above approach
import java.util.*;
public class gfg
{
// function to print kth border of a matrix
static void printkthborder(int [][]mat, int n, int k)
{
    for (int i = 0; i < n; i  )
    {
        system.out.println();
        for (int j = 0; j < n; j  )
        {
            // to keep track of which element to skip
            int flag = 0;
            if ((i == k - 1 || i == n - k) &&
                (j >= k - 1 && j <= n - k)) {
                // print the element
                system.out.print(mat[i][j]   " ");
                flag = 1;
            }
            else if ((j == k - 1 || j == n - k) &&
                    (i >= k - 1 && i <= n - k)) {
                // print the element
                system.out.print(mat[i][j]   " ");
                flag = 1;
            }
            if (flag == 0)
                system.out.print("  ");
        }
    }
}
// driver code
public static void main(string args[]) {
    int n = 5;
    int k = 1;
    int [][]mat = {{1, 2, 3, 4, 5},
                    {6, 7, 8, 9, 10},
                    {11, 12, 13, 14, 15},
                    {16, 17, 18, 19, 20},
                    {21, 22, 23, 24, 25}};
    printkthborder(mat, n, k);
}
}
// this code is contributed by samim hossain mondal.

python 3

# python program for above approach
# function to print kth border of a matrix
def printkthborder(mat, n, k):
    for i in range(n):
        print()
        for j in range(n):
            # to keep track of which element to skip
            flag = 0
            if((i == k-1 or i == n-k) \
                  and (j >= k-1 and j <= n-k)):
                # print the element
                print(mat[i][j], end =" ")
                flag = 1
            elif (j == k-1 or j == n-k) \
                  and (i >= k-1 and i <= n-k):
                # print the element
                print(mat[i][j], end =" ")
                flag = 1
            if flag == 0:
                print(end ="  ")
# driver code
n = 5
k = 1
mat = [[1, 2, 3, 4, 5], \
       [6, 7, 8, 9, 10], \
       [11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20], \
       [21, 22, 23, 24, 25]]
printkthborder(mat, n, k)

c

// c# program to implement
// the above approach
using system;
class gfg
{
// function to print kth border of a matrix
static void printkthborder(int [,]mat, int n, int k)
{
    for (int i = 0; i < n; i  )
    {
        console.writeline();
        for (int j = 0; j < n; j  )
        {
            // to keep track of which element to skip
            int flag = 0;
            if ((i == k - 1 || i == n - k) &&
                (j >= k - 1 && j <= n - k)) {
                // print the element
                console.write(mat[i, j]   " ");
                flag = 1;
            }
            else if ((j == k - 1 || j == n - k) &&
                    (i >= k - 1 && i <= n - k)) {
                // print the element
                console.write(mat[i, j]   " ");
                flag = 1;
            }
            if (flag == 0)
                console.write("  ");
        }
    }
}
// driver code
public static void main() {
    int n = 5;
    int k = 1;
    int [,]mat = {{1, 2, 3, 4, 5},
                    {6, 7, 8, 9, 10},
                    {11, 12, 13, 14, 15},
                    {16, 17, 18, 19, 20},
                    {21, 22, 23, 24, 25}};
    printkthborder(mat, n, k);
}
}
// this code is contributed by samim hossain mondal.

java 描述语言


output

1   2  3  4  5 
6           10 
11          15 
16          20 
21 22 23 24 25 

时间复杂度:o(n^2) t3】空间复杂度: o(1)