我正在尝试采用 9x9、12x12、15x15 等数组,并让程序将它们解释为多个 3x3 正方形。
例如:
0 0 1 0 0 0 0 0 0
0 0 0 0 0 2 0 0 0
0 0 0 0 0 0 0 3 0
0 0 0 0 0 0 6 0 0
0 0 4 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0 0
0 0 0 0 8 0 0 0 9
将被理解为:
0 0 1 | 0 0 0 | 0 0 0
0 0 0 | 0 0 2 | 0 0 0
0 0 0 | 0 0 0 | 0 3 0
------+-------+------
0 0 0 | 0 0 0 | 6 0 0
0 0 4 | 0 0 0 | 0 0 0
0 0 0 | 0 0 5 | 0 0 0
------+-------+------
0 0 0 | 0 0 0 | 0 0 0
0 7 0 | 0 0 0 | 0 0 0
0 0 0 | 0 8 0 | 0 0 9
地点:
"1" @ [0][2] is in box "[0][0]"
"2" @ [1][5] is in box "[0][1]"
...
"6" @ [3][6] is in box "[1][2]"
...
"9" @ [8][8] is in box "[2][2]"
.
我可以使用row % 3
和column % 3
来确定框中的行和列值,但是如何确定给定值在哪个框中数组存储在?
这个公式可以用在如下方法中。
public int[] determineCoordinatesOfBox(int rowInArray, int columnColumnInArray) {
// determine row value
// determine column value
// return new int[2] with coordinates
}
这似乎是可能的,我一直在为此绞尽脑汁。也许我把一个简单的问题变得太难了?
非常感谢您的帮助!
- 贾斯蒂安
最佳答案
您正在寻找 /
运算符:
box[0] = rowInArray / 3;
box[1] = columnInArray / 3;
关于java - 算术运算使 0、1 和 2 返回 0 | 3、4 和 5 返回 1 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3348025/