算术运算使得 0, 1, & 2 返回 0 | 3、4、& 5返回1等
我试图采用 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
}
这似乎是可能的,我一直在为此绞尽脑汁。也许我把一个简单的问题变得太难了?
非常感谢您的帮助!
- 查士丁
I'm trying to take 9x9, 12x12, 15x15, etc. arrays and have the program interpret them as multiple 3x3 squares.
For example:
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
Will be understood as:
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
Where:
"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]"
.
I can use row % 3
and column % 3
to determine the row and column values within the box, but how can I determine which box a given value in the array is stored in?
This formula could be used in a method such as the one below.
public int[] determineCoordinatesOfBox(int rowInArray, int columnColumnInArray) {
// determine row value
// determine column value
// return new int[2] with coordinates
}
It seems possible and I've been beating my head over this. Perhaps I'm making a simple problem too difficult?
Many thanks for the help!
- Justian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找
/
运算符:You're looking for the
/
operator:如果我理解正确的话,这只是简单的整数除法。
由于您正在编写 Java 代码(至少在 C、C++ 和 C# 中是相同的),因此它只是
/
运算符:只需保留除法整数的两个参数 -
7 / 3< /code> 是
2
,但7 / 3.0
或7.0 / 3
将是2.5
。If I understand correctly, it's just simple integer division.
Since you're coding Java (it would be the same in at least C, C++ and C#), it's simply
/
operator:Just keep both the arguments of division integer -
7 / 3
is2
, but7 / 3.0
or7.0 / 3
will be2.5
.