📌  相关文章
📜  从矩阵中删除所有角落X行和列

📅  最后修改于: 2021-05-31 22:06:01             🧑  作者: Mango

给定一个NxN矩阵和一个整数X。任务是从NxN矩阵中删除X行和X列。

  • 从矩阵中删除前X行和列。

例子:

Input: n = 4, 
       arr[][] = {{20, 2, 10, 16},
                  {20, 17, 11, 6},
                  {14, 16, 1, 3},
                  {10, 2, 17, 4}}, 
       x = 2
Output:
1 3
17 4

Explanation:
Here the value of X is 2.
So remove the first 2 rows 
and the first 2 columns from the matrix. 
Hence the output is
1 3 
17 4

简单方法:

  • 跳过前x行和列,并在矩阵中打印其余元素。
  • 从第x行开始打印,直到第n-1行。
  • 从第x列开始打印,直到第n-1列。

执行:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to remove first x
// rows and columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving First " << x
         << " rows and columns:\n";
 
    // Start from the xth row
    // and print till n-1th row
    for (int i = x; i < n; i++) {
 
        // Start from the xth column
        // and print till the n-1th column
        for (int j = x; j < n; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
     
// Function to remove first x
// rows and columns from an array
static void remove_X_Rows_and_Columns(int a[][],
                                      int n, int x)
{
    System.out.print("\nRemoving First " + x +
                     " rows and columns:\n");
 
    // Start from the xth row
    // and print till n-1th row
    for(int i = x; i < n; i++)
    {
         
        // Start from the xth column
        // and print till the n-1th column
        for(int j = x; j < n; j++)
        {
             
            // Accessing the array using pointers
            System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Print the matrix
static void printMatrix(int a[][], int n)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            System.out.print(a[ i][j] + " ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String [] args)
{
    int n = 4;
 
    // Get the array inputs
    int a[][] = new int[n][n];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    System.out.println( "Original Matrix:");
    printMatrix(a, n);
 
    int x = 2;
 
    // Passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns(a, n, x);
}
}
 
// This code is contributed by chitranayal


Python3
# Python3 implementation of the above approach
  
# Function to remove first x
# rows and columns from an array
def remove_X_Rows_and_Columns(a, n, x):
 
    print("Removing First ", x,
          " rows and columns:")
 
    # Start from the xth row
    # and pr till n-1th row
    for i in range(x, n):
 
        # Start from the xth column
        # and pr till the n-1th column
        for j in range(x, n):
 
            # Accessing the array using poers
            print(a[i][j], end = " ")
 
        print()
     
# Print the matrix
def prMatrix(a, n):
 
    for i in range(n):
        for j in range(n):
            print(a[i][j], end = " ")
 
        print()
     
# Driver Code
if __name__ == '__main__':
 
    n = 4
 
    # get the array inputs
    a = [[0 for i in range(n)]
            for i in range(n)]
    for i in range(n):
        for j in range(n):
            a[i][j] = (i * 10 + j)
         
    # Print the matrix
    print("Original Matrix:")
    prMatrix(a, n)
 
    x = 2
 
    # passing the two dimensional
    # array using a single poer
    remove_X_Rows_and_Columns(a, n, x)
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to remove first x
// rows and columns from an array
static void remove_X_Rows_and_Columns(int[,] a, 
                                      int n, int x)
{
    Console.WriteLine("\nRemoving First " + x +
                      " rows and columns:\n");
     
    // Start from the xth row
    // and print till n-1th row
    for(int i = x; i < n; i++)
    {
         
        // Start from the xth column
        // and print till the n-1th column
        for(int j = x; j < n; j++)
        {
             
            // Accessing the array using pointers
            Console.Write(a[i, j] + " ");
             
        }
        Console.WriteLine();
    }
}
 
// Print the matrix
static void printMatrix(int[,] a, int n)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            Console.Write(a[i, j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
static public void Main()
{
    int n = 4;
     
    // Get the array inputs
    int[,] a = new int[n, n];
     
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            a[i,j] = (i * 10 + j);
        }
    }
     
    // Print the matrix
    Console.WriteLine( "Original Matrix:");
    printMatrix(a, n);
    int x = 2;
     
    // Passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns(a, n, x);
}
}
 
// This code is contributed by avanitrachhadiya2155


PHP


Javascript


C++
#include 
using namespace std;
 
// Function to remove last x rows
// and columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving Last " << x
         << " rows and columns:\n";
 
    // Start from the 0th row
    // and print till n-xth row
    for (int i = 0; i < n - x; i++) {
 
        // Start from the 0th column
        // and print till the n-xth column
        for (int j = 0; j < n - x; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}


Python3
# Function to remove last x rows
# and columns from an array
def remove_X_Rows_and_Columns(a, n, x):
    print("\nRemoving Last", x, "rows and columns:")
     
    # Start from the 0th row
    # and print till n-xth row
    for i in range(n - x):
       
        # Start from the 0th column
        # and print till the n-xth column
        for j in range(n - x):
           
            # Accessing the array using pointers
            print(a[i][j], end = " ")
        print()
 
# Print the matrix
def printMatrix(a, n):
    for i in range(n):
        for j in range(n):
            print(a[i][j], end = " ")
        print()
 
n = 4
 
# get the array inputs
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = (i * 10 + j)
 
# Print the matrix
print("Original Matrix:")
printMatrix(a, n)
x = 2
 
# passing the two dimensional
# array using a single pointer
remove_X_Rows_and_Columns(a, n, x)
 
# This code is contributed by rag2127


C++
#include 
using namespace std;
 
// Function to remove first x rows
// and last x columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving First " << x
         << " rows and Last " << x
         << " columns:\n";
 
    // Start from the xth row
    // and print till n-1th row
    for (int i = x; i < n; i++) {
 
        // Start from the 0th column
        // and print till the n-xth column
        for (int j = 0; j < n - x; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}


C++
#include 
using namespace std;
 
// Function to remove last x rows
// and first x columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving Last " << x
         << " rows and First " << x
         << " columns:\n";
 
    // Start from the 0th row
    // and print till n-xth row
    for (int i = 0; i < n - x; i++) {
 
        // Start from the xth column and
        // print till the n-1th column
        for (int j = x; j < n; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}


Python3
# Function to remove last x rows
# and first x columns from an array
def remove_X_Rows_and_Columns(a, n, x):
 
    print("\nRemoving Last ", x ,
          " rows and First ", x,
          " columns:")
 
    # Start from the 0th row
    # and print till n-xth row
    for i in range(n - x):
 
        # Start from the xth column and
        # print till the n-1th column
        for j in range(x, n):
 
            # Accessing the array using pointers
            print(a[i][j], end = " ")
             
        print()
 
# Print the matrix
def printMatrix(a, n):
 
    for i in range(n):
        for j in range(n):
            print(a[i][j], end = " ")
             
        print()
 
# Driver code
if __name__ == '__main__':
     
    n = 4
 
    # Get the array inputs
    a = [[0 for i in range(n)]
            for i in range(n)]
             
    for i in range(n):
        for j in range(n):
            a[i][j] = (i * 10 + j)
 
    # Print the matrix
    print("Original Matrix:")
    printMatrix(a, n)
 
    x = 2
 
    # Passing the two dimensional
    # array using a single pointer
    remove_X_Rows_and_Columns(a, n, x)
 
# This code is contributed by mohit kumar 29


输出:
Original Matrix:
0 1 2 3 
10 11 12 13 
20 21 22 23 
30 31 32 33 

Removing First 2 rows and columns:
22 23 
32 33
  • 从矩阵中删除最后的X行和列。

例子:

Input: n = 4, 
       arr[][] = {{20, 2, 10, 16},
                  {20, 17, 11, 6},
                  {14, 16, 1, 3},
                  {10, 2, 17, 4}}, 
       x = 2
Output:
20 2
20 17

Explanation:
Here the value of X is 2.
So remove the last 2 rows and 
the last 2 columns from the matrix. 
Hence the output is
20 2
20 17

简单方法

  • 跳过最后的x行和列,并打印矩阵中的其余元素。
  • 从第0行开始打印,直到第n-x行。
  • 从第0列开始打印,直到第n-x列。

执行:

C++

#include 
using namespace std;
 
// Function to remove last x rows
// and columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving Last " << x
         << " rows and columns:\n";
 
    // Start from the 0th row
    // and print till n-xth row
    for (int i = 0; i < n - x; i++) {
 
        // Start from the 0th column
        // and print till the n-xth column
        for (int j = 0; j < n - x; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}

Python3

# Function to remove last x rows
# and columns from an array
def remove_X_Rows_and_Columns(a, n, x):
    print("\nRemoving Last", x, "rows and columns:")
     
    # Start from the 0th row
    # and print till n-xth row
    for i in range(n - x):
       
        # Start from the 0th column
        # and print till the n-xth column
        for j in range(n - x):
           
            # Accessing the array using pointers
            print(a[i][j], end = " ")
        print()
 
# Print the matrix
def printMatrix(a, n):
    for i in range(n):
        for j in range(n):
            print(a[i][j], end = " ")
        print()
 
n = 4
 
# get the array inputs
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = (i * 10 + j)
 
# Print the matrix
print("Original Matrix:")
printMatrix(a, n)
x = 2
 
# passing the two dimensional
# array using a single pointer
remove_X_Rows_and_Columns(a, n, x)
 
# This code is contributed by rag2127
输出:
Original Matrix:
0 1 2 3 
10 11 12 13 
20 21 22 23 
30 31 32 33 

Removing Last 2 rows and columns:
0 1 
10 11
  • 从矩阵中删除前X行和后X列。

例子:

Input: n = 4, 
       arr[][] = {{20, 2, 10, 16},
                  {20, 17, 11, 6},
                  {14, 16, 1, 3},
                  {10, 2, 17, 4}}, 
       x = 2
Output:
14 16
10 2

Explanation:
Here the value of X is 2.
So remove the first 2 rows 
and the last 2 columns from the matrix. 
Hence the output is
14 16
10 2

简单方法

  • 跳过前x行和后x列,并在矩阵中打印其余元素。
  • 从第x行开始打印,直到第n-1行。
  • 从第0列开始打印,直到第n-x列。

执行:

C++

#include 
using namespace std;
 
// Function to remove first x rows
// and last x columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving First " << x
         << " rows and Last " << x
         << " columns:\n";
 
    // Start from the xth row
    // and print till n-1th row
    for (int i = x; i < n; i++) {
 
        // Start from the 0th column
        // and print till the n-xth column
        for (int j = 0; j < n - x; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}
输出:
Original Matrix:
0 1 2 3 
10 11 12 13 
20 21 22 23 
30 31 32 33 

Removing First 2 rows and Last 2 columns:
20 21 
30 31
  • 从矩阵中删除最后X行和前x列。

例子:

Input: n = 4, 
       arr[][] = {{20, 2, 10, 16},
                  {20, 17, 11, 6},
                  {14, 16, 1, 3},
                  {10, 2, 17, 4}}, 
       x = 2
Output:
10 16
11 6

Explanation:
Here the value of X is 2.
So remove the last 2 rows and
the first 2 columns from the matrix. 
Hence the output is
10 16
11 6

简单方法

  • 跳过最后x行和前x列,并在矩阵中打印其余元素。
  • 从第0行开始打印,直到第n-x行。
  • 从第x列开始打印,直到第n-1列。

执行:

C++

#include 
using namespace std;
 
// Function to remove last x rows
// and first x columns from an array
void remove_X_Rows_and_Columns(int* a, int n, int x)
{
 
    cout << "\nRemoving Last " << x
         << " rows and First " << x
         << " columns:\n";
 
    // Start from the 0th row
    // and print till n-xth row
    for (int i = 0; i < n - x; i++) {
 
        // Start from the xth column and
        // print till the n-1th column
        for (int j = x; j < n; j++) {
 
            // Accessing the array using pointers
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
// Print the matrix
void printMatrix(int* a, int n)
{
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << *((a + i * n) + j) << " ";
        }
        cout << endl;
    }
}
 
int main()
{
    int n = 4;
 
    // get the array inputs
    int a[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            a[i][j] = (i * 10 + j);
        }
    }
 
    // Print the matrix
    cout << "Original Matrix:\n";
    printMatrix((int*)a, n);
 
    int x = 2;
 
    // passing the two dimensional
    // array using a single pointer
    remove_X_Rows_and_Columns((int*)a, n, x);
 
    return 0;
}

Python3

# Function to remove last x rows
# and first x columns from an array
def remove_X_Rows_and_Columns(a, n, x):
 
    print("\nRemoving Last ", x ,
          " rows and First ", x,
          " columns:")
 
    # Start from the 0th row
    # and print till n-xth row
    for i in range(n - x):
 
        # Start from the xth column and
        # print till the n-1th column
        for j in range(x, n):
 
            # Accessing the array using pointers
            print(a[i][j], end = " ")
             
        print()
 
# Print the matrix
def printMatrix(a, n):
 
    for i in range(n):
        for j in range(n):
            print(a[i][j], end = " ")
             
        print()
 
# Driver code
if __name__ == '__main__':
     
    n = 4
 
    # Get the array inputs
    a = [[0 for i in range(n)]
            for i in range(n)]
             
    for i in range(n):
        for j in range(n):
            a[i][j] = (i * 10 + j)
 
    # Print the matrix
    print("Original Matrix:")
    printMatrix(a, n)
 
    x = 2
 
    # Passing the two dimensional
    # array using a single pointer
    remove_X_Rows_and_Columns(a, n, x)
 
# This code is contributed by mohit kumar 29
输出:
Original Matrix:
0 1 2 3 
10 11 12 13 
20 21 22 23 
30 31 32 33 

Removing Last 2 rows and First 2 columns:
2 3 
12 13
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”