📜  程序以矩阵形式打印元素,逐行跳过备用元素

📅  最后修改于: 2021-05-31 20:04:44             🧑  作者: Mango

给定一个N * N大小的矩阵(其中N始终为奇数),任务是打印逐行跳过备用元素的矩阵元素。
例子

Input: mat[3][3] = 
{{1, 4, 3},
 {2, 5, 6},
 {7, 8, 9}}
Output: 1 3 5 7 9

Input: mat[5][5] = 
{{1, 2, 3, 4, 9},
 {5, 6, 7, 8, 44},
 {19, 10, 11, 12, 33},
 {13, 14, 15, 16, 55},
 {77, 88, 99, 111, 444}}
Output: 1 3 9 6 8 19 11 33 14 16 77 99 444 

方法:

  1. 开始遍历矩阵。
  2. 对于每一行,请检查行号是偶数还是奇数。
    • 如果该行是偶数,则在该行的偶数索引处打印元素。
    • 如果该行是奇数,请在该行的奇数索引处打印元素。

下面是上述方法的实现:

C++
// C++ program to print elements of a Matrix
// row-wise skipping alternate elements
#include 
using namespace std;
#define R 100
#define C 100
 
// Function to print the alternate
// elements of a matrix
void printElements(int mat[][C], int n)
{
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0)
            for (int j = 0; j < n; j += 2)
                cout << mat[i][j] << " ";
        else
            for (int j = 1; j < n; j += 2)
                cout << mat[i][j] << " ";
    }
}
 
// Driver code
int main()
{
    int n = 3;
    int mat[R][C] = { { 1, 5, 3 },
                      { 2, 4, 7 },
                      { 9, 8, 6 } };
 
    printElements(mat, n);
 
    return 0;
}


Java
// Java Program to print elements of a Matrix
// row-wise skipping alternate elements
import java.util.*;
import java.lang.*;
 
class GFG{
// Function to print the alternate
// elements of a matrix
static void printElements(int[][] mat, int n)
{
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
            for (int j = 0; j < n; j += 2)
                System.out.print(mat[i][j] + " ");
        else
            for (int j = 1; j < n; j += 2)
                System.out.print(mat[i][j] + " ");
    }
}
 
// Driver code
public static void main(String args[])
{
    int n = 3;
    int[][] mat = new int[][]{ { 1, 5, 3 },{ 2, 4, 7 },{ 9, 8, 6 } };
 
    printElements(mat, n);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# Python 3 program to print elements of a Matrix
# row-wise skipping alternate elements 
 
# Function to print the alternate
# elements of a matrix
def printElements(mat, n) :
 
    for i in range(n) :
        if i % 2 == 0 :
            for j in range(0, n, 2) :
                print(mat[i][j],end = " ")
        else :
            for j in range(1, n, 2) :
                print(mat[i][j],end =" ")
             
     
# Driver Code
if __name__ == "__main__" :
 
    n = 3
    mat = [ [ 1, 5, 3],
            [ 2, 4, 7],
            [ 9, 8, 6] ]
 
    printElements(mat , n)
 
 
# This code is contributed by ANKITRAI1


C#
// C# Program to print elements
// of a Matrix row-wise skipping
// alternate elements
using System;
 
class GFG
{
// Function to print the alternate
// elements of a matrix
static void printElements(int[,] mat,
                          int n)
{
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
            for (int j = 0; j < n; j += 2)
                Console.Write(mat[i, j] + " ");
        else
            for (int j = 1; j < n; j += 2)
                Console.Write(mat[i, j] + " ");
    }
}
 
// Driver code
public static void Main()
{
    int n = 3;
    int[,] mat = new int[,]{ { 1, 5, 3 },
                             { 2, 4, 7 },
                             { 9, 8, 6 }};
 
    printElements(mat, n);
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


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