<分区>
可以按照下图打印矩阵值
<分区>
可以按照下图打印矩阵值
最佳答案
据我了解,可以使用以下计算下一个条目的索引的辅助函数来解决该问题。语法是 C#,但在 Java 中应该类似,其中 m
和 n
应该是各自的矩阵维度。这个想法是检查所需的主要方向是左下还是右上;如果到达矩阵的边缘(必须事先检查),则分别将其修改为向右和向下。该实现假设行和列的索引从零开始。
public struct Cell
{
public int Row;
public int Col;
}
public static Cell GetNext(Cell iCell)
{
Cell Result;
if (( iCell.Row + iCell.Col ) % 2 == 0)
{
if (iCell.Col == n - 1)
Result = new Cell { Row = iCell.Row + 1, Col = n - 1 };
else if (iCell.Row == 0)
Result = new Cell { Row = 0, Col = iCell.Col + 1 };
else
Result = new Cell { Row = iCell.Row - 1, Col = iCell.Col + 1 };
}
else
{
if (iCell.Row == m - 1)
Result = new Cell { Row = m - 1, Col = iCell.Col + 1 };
else if (iCell.Col == 0)
Result = new Cell { Row = iCell.Row + 1, Col = 0 };
else
Result = new Cell { Row = iCell.Row + 1, Col = iCell.Col - 1 };
}
return Result;
}
关于java - 矩阵 : get value from given matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38233852/