一个C程序遍历一个n * n的矩阵 [英] A c program to traverse a n*n matrix

查看:156
本文介绍了一个C程序遍历一个n * n的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们具有例如我们取n = 4 n * n的矩阵和矩阵如下所示。

We have a n*n matrix for example we take n=4 and the matrix is given below.

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

我们必须遍历它的顺序:

We have to traverse it in the sequence:

1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10

我怎样才能做到这一点?

How can I do this?

推荐答案

运行可能意味着访问和打印每个entry.You想通过它顺时针螺旋状从顶部开始。

"Traversing" in this case probably means accessing and printing each entry.You want to go through it in a clockwise spiral, starting from the top.

下面是你需要做一个英语句子式的描述:

Here is an English-sentence-style description of what you need to do:

如果你能找到一种方法,去子矩阵的左上角元素,顶行左到右读出的条目,读出在右列中的条目顶部至底部,底行中的条目从右到左,然后左栏底部到顶部,则有一个迭代。你可以把剩余的子矩阵,并继续下去,直到你已经一无所有。

If you can find a way to go to the TOP-LEFT element of a sub-matrix, read off the entries in the top row left-to-right, read off the entries in the right column top-to-bottom, entries in the bottom row right-to-left, and then the left column bottom-to-top, you have one iteration. You can take the remaining sub-matrix and continue until you have nothing left.

另外提示:

从细胞M [X] [Y]

From the cell M[x][y],


  • M [X] [Y + 1]是细胞的权利(只要Y + 1< N)

  • M [X] [Y-1]是左侧单元格(只要Y> 0)

  • M [X + 1] [Y]是下面的单元格(只要X + 1< N)

  • M [X-1] [Y]为单元格上方(只要当x> 0)

这篇关于一个C程序遍历一个n * n的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆