算术运算使得 0, 1, & 2 返回 0 | 3、4、& 5返回1等

发布于 2024-09-11 15:24:01 字数 1134 浏览 2 评论 0原文

我试图采用 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 % 3column % 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2024-09-18 15:24:01

您正在寻找 / 运算符:

box[0] = rowInArray / 3;
box[1] = columnInArray / 3;

You're looking for the / operator:

box[0] = rowInArray / 3;
box[1] = columnInArray / 3;
尾戒 2024-09-18 15:24:01

如果我理解正确的话,这只是简单的整数除法。

由于您正在编写 Java 代码(至少在 C、C++ 和 C# 中是相同的),因此它只是 / 运算符:

int rowInArray = 3;
int columnInArray = 7;

int boxY = rowInArray / 3;    // will evaluate to 1
int boxX = columnInArray / 3; // will evaluate to 2

int rowInBox = rowInArray % 3;       // will evaluate to 0
int columnInBox = columnInArray % 3; // will evaluate to 1

只需保留除法整数的两个参数 - 7 / 3< /code> 是 2,但 7 / 3.07.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:

int rowInArray = 3;
int columnInArray = 7;

int boxY = rowInArray / 3;    // will evaluate to 1
int boxX = columnInArray / 3; // will evaluate to 2

int rowInBox = rowInArray % 3;       // will evaluate to 0
int columnInBox = columnInArray % 3; // will evaluate to 1

Just keep both the arguments of division integer - 7 / 3 is 2, but 7 / 3.0 or 7.0 / 3 will be 2.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文