给定一个整数X和一个正方形矩阵mat [] [] ,任务是从给定矩阵中删除前X行和列,并打印更新后的矩阵。
例子:
Input: mat[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{8, 9, 4, 2},
{4, 8, 9, 2} },
X = 2
Output:
4 2
9 2
Input: mat[][] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9} },
X = 1
Output:
5 6
8 9
方法:为所有i,j∈[X,N – 1]打印矩阵arr [i] [j]的元素,其中N是给定方阵的阶数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX = 50;
// Function to print the matrix after
// ignoring first x rows and columns
void remove_row_col(int arr[][MAX], int n, int x)
{
// Ignore first x rows and columns
for (int i = x; i < n; i++) {
for (int j = x; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
// Order of the square matrix
int n = 3;
int arr[][MAX] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int x = 1;
remove_row_col(arr, n, x);
}
Java
// Java implementation of the approach
import java.io.*;
class GFG
{
static int MAX = 50;
// Function to print the matrix after
// ignoring first x rows and columns
static void remove_row_col(int arr[][], int n, int x)
{
// Ignore first x rows and columns
for (int i = x; i < n; i++)
{
for (int j = x; j < n; j++)
{
System.out.print( arr[i][j] + " ");
}
System.out.println();
}
}
// Driver Code
public static void main (String[] args)
{
// Order of the square matrix
int n = 3;
int arr[][] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int x = 1;
remove_row_col(arr, n, x);
}
}
// This code is contributed by
// shk
Python3
# Python3 implementation of the approach
# Function to print the matrix after
# ignoring first x rows and columns
def remove_row_col(arr, n, x):
# Ignore first x rows and columns
for i in range(x, n):
for j in range(x, n):
print(arr[i][j], end = " ")
print()
# Driver Code
if __name__ == "__main__":
# Order of the square matrix
n = 3
MAX = 50
arr = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
x = 1
remove_row_col(arr, n, x)
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the matrix after
// ignoring first x rows and columns
static void remove_row_col(int [,]arr, int n, int x)
{
// Ignore first x rows and columns
for (int i = x; i < n; i++)
{
for (int j = x; j < n; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
// Order of the square matrix
int n = 3;
int [,]arr = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int x = 1;
remove_row_col(arr, n, x);
}
}
// This code is contributed by Ryuga
PHP
Javascript
输出:
5 6
8 9
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。