原文:

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

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

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

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

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 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
?>

java 描述语言


输出:

1 2 3 1 2 3 4 5 6

时间复杂度:o(r * c) t3】空间复杂度: o(1) 本文由丹麦语 _raza 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。