📌  相关文章
📜  以交替方式打印矩阵(从左到右然后从右到左)

📅  最后修改于: 2022-05-13 01:57:21.889000             🧑  作者: Mango

以交替方式打印矩阵(从左到右然后从右到左)

给定一个二维数组,任务是以交替方式打印二维(第一行从左到右,然后从右到左,依此类推)。
例子:

Input : arr[][2] = {{1, 2}
                    {2, 3}}; 
Output : 1  2  3  2 
 
Input :arr[][3] = { { 7 , 2 , 3 },
                    { 2 , 3 , 4 },
                    { 5 , 6 ,  1 }}; 
Output : 7  2   3  4  3  2  5  6  1

这个问题的解决方案是运行两个循环并以从左到右和从右到左的方式打印行。我们维护一个标志来查看当前行是应该从左到右还是从右到左打印。我们在每次迭代后切换标志。

C++
// C++ program to print matrix in alternate manner
#include
using namespace std;
#define R 3
#define C 3
 
// Function for print matrix in alternate manner
void convert(int arr[R][C])
{
    bool leftToRight = true;
    for (int i=0; i=0; j--)
                printf("%d ",arr[i][j]);
        }
 
        leftToRight = !leftToRight;
    }
}
 
// Driver code
int main()
{
    int arr[][C] =
    {
        { 1 , 2 , 3 },
        { 3 , 2 , 1 },
        { 4 , 5 , 6 },
    };
 
    convert(arr);
    return 0;
}


Java
//Java program to print matrix in alternate manner
class GFG {
 
    static final int R = 3;
    static final int C = 3;
 
// Function for print matrix in alternate manner
    static void convert(int arr[][]) {
        boolean leftToRight = true;
        for (int i = 0; i < R; i++) {
            if (leftToRight) {
                for (int j = 0; j < C; j++) {
                    System.out.printf("%d ", arr[i][j]);
                }
            } else {
                for (int j = C - 1; j >= 0; j--) {
                    System.out.printf("%d ", arr[i][j]);
                }
            }
 
            leftToRight = !leftToRight;
        }
    }
 
// Driver code
    static public void main(String[] args) {
        int arr[][]
                = {
                    {1, 2, 3},
                    {3, 2, 1},
                    {4, 5, 6},};
 
        convert(arr);
    }
}
 
// This code is contributed by Rajput-Ji


Python 3
# Python 3 program to print matrix
# in alternate manner
R = 3
C = 3
 
# Function for print matrix
# in alternate manner
def convert(arr):
 
    leftToRight = True
    for i in range(R):
        if (leftToRight):
            for j in range(C):
                print(arr[i][j], end = " ")
     
        else:
            for j in range(C - 1, -1, -1):
                print(arr[i][j], end = " ")
 
        leftToRight = not leftToRight
 
# Driver code
if __name__ == "__main__":
    arr =[[ 1 , 2 , 3 ],
          [ 3 , 2 , 1 ],
          [ 4 , 5 , 6 ]]
 
    convert(arr)
 
# This code is contributed
# by ChitraNayal


C#
//C# program to print matrix in alternate manner
using System;
public class GFG {
  
    static readonly int R = 3;
    static readonly int C = 3;
  
// Function for print matrix in alternate manner
    static void convert(int [,]arr) {
        bool leftToRight = true;
        for (int i = 0; i < R; i++) {
            if (leftToRight) {
                for (int j = 0; j < C; j++) {
                    Console.Write(arr[i,j]+" ");
                }
            } else {
                for (int j = C - 1; j >= 0; j--) {
                    Console.Write(arr[i,j]+" ");
                }
            }
  
            leftToRight = !leftToRight;
        }
    }
  
// Driver code
    static public void Main() {
        int [,]arr
                = {
                    {1, 2, 3},
                    {3, 2, 1},
                    {4, 5, 6},};
  
        convert(arr);
    }
}
  
// This code is contributed by Rajput-Ji


PHP
= 0; $j--)
                echo $arr[$i][$j], " ";
        }
 
        $leftToRight = !$leftToRight;
    }
}
 
// Driver code
$arr = array(array(1 , 2 , 3 ),
             array(3 , 2 , 1 ),
             array(4 , 5 , 6 ));
 
convert($arr);
 
// This code is contributed by ajit
?>


Javascript


输出:

1 2 3 1 2 3 4 5 6