检查数字是否与小键盘中的另一个数字位于同一行的最简单方法
Simplest way to check if a number is in the same row as another in a numpad
标题中很难解释清楚。我有两个数字,需要找到它们都在数字键盘的同一行中:
7 8 9
4 5 6
1 2 3
所以一行将是 7、8、9 或 1、2、3 等。如果我有数字 1 和 5 那将是负数,因为它们不在同一行,而如果我有数字 7 和 9 是正数,因为它们在同一行。我有一种感觉,我缺少某种简单的数学可以让我得到答案。
同样,我需要在单独的查询中查明两个数字是否在同一列中。一列当然是 1、4、7 或 3、6、9。
我发现自己唯一的解决办法是制作所有行和列的数组,如 [4, 5, 6]
等,然后检查这两个数字是否在同一个数组中,但似乎有点冗长.
我的解决方案已经是 simplest/shortest 还是有更好的解决方案?
编辑:编辑以更清楚地表明我想要最简单的而不一定是最好的。由于不清楚而明显错误地关闭?因为已经有了一个答案,这正是我要找的。
顺便说一下,“最佳”是一个非常含糊的词。它可能意味着最有效、最美观,甚至可能是完成上述任务的“最酷”方式。我不确定你在找什么。但是,在我看来,做到这一点最酷的方法,也是未来案例的最佳方法,是能够找到元素的行和列,然后将这些值与您正在寻找的其他元素进行比较;在你的例子中是 4 和 7 这样的数字。
这会同时给你两个答案。在下面的示例中,我们使用通用的 4x5 矩阵来展示此解决方案如何适用于您的特定用例并且是 future-proof.
import java.util.*;
public class abc {
public static void main(String[] args) {
int nums[][] = {{12, 20, 30, 40},
{15, 25, 35, 45},
{24, 29, 39, 51},
{35, 30, 39, 50},
{50, 60, 75, 72}};
int rows = 5;
int search_element = 39;
int ans[] = Saddleback(nums, rows - 1, 0, search_element);
System.out.println("Position of "+search_element+" in the matrix is ("+ans[0] + "," + ans[1]+")");
}
/**
* @param nums, the matrix.
* @param row the current row.
* @param col the current column.
* @param search_element the element that we want to search for.
* @return value: If found the index(row and column) of the element.
* else return (-1 -1).
*/
private static int[] Saddleback(int nums[][], int row, int col, int search_element) {
//numsay to store the row and column of the searched element
int element_pos[] = {-1, -1};
if (row < 0 || col >= nums[row].length) {
return element_pos;
}
if (nums[row][col] == search_element) {
element_pos[0] = row;
element_pos[1] = col;
return element_pos;
}
//move up if the current element is greater than the given element
else if (nums[row][col] > search_element) {
return Saddleback(nums, row - 1, col, search_element);
}
//otherwise move right
return Saddleback(nums, row, col + 1, search_element);
}
/**
* Main method
*
* @param args Command line arguments
*/
}
是的,有一个快速的数学方法。
两个整数在同一列如果num1 % 3 == num2 % 3
两个整数在同一行如果(num1 - 1) / 3 == (num2 - 1) / 3
对于 row-check 重要的是它们是整数,因为整数除法会舍去余数。
标题中很难解释清楚。我有两个数字,需要找到它们都在数字键盘的同一行中:
7 8 9
4 5 6
1 2 3
所以一行将是 7、8、9 或 1、2、3 等。如果我有数字 1 和 5 那将是负数,因为它们不在同一行,而如果我有数字 7 和 9 是正数,因为它们在同一行。我有一种感觉,我缺少某种简单的数学可以让我得到答案。
同样,我需要在单独的查询中查明两个数字是否在同一列中。一列当然是 1、4、7 或 3、6、9。
我发现自己唯一的解决办法是制作所有行和列的数组,如 [4, 5, 6]
等,然后检查这两个数字是否在同一个数组中,但似乎有点冗长.
我的解决方案已经是 simplest/shortest 还是有更好的解决方案?
编辑:编辑以更清楚地表明我想要最简单的而不一定是最好的。由于不清楚而明显错误地关闭?因为已经有了一个答案,这正是我要找的。
顺便说一下,“最佳”是一个非常含糊的词。它可能意味着最有效、最美观,甚至可能是完成上述任务的“最酷”方式。我不确定你在找什么。但是,在我看来,做到这一点最酷的方法,也是未来案例的最佳方法,是能够找到元素的行和列,然后将这些值与您正在寻找的其他元素进行比较;在你的例子中是 4 和 7 这样的数字。
这会同时给你两个答案。在下面的示例中,我们使用通用的 4x5 矩阵来展示此解决方案如何适用于您的特定用例并且是 future-proof.
import java.util.*;
public class abc {
public static void main(String[] args) {
int nums[][] = {{12, 20, 30, 40},
{15, 25, 35, 45},
{24, 29, 39, 51},
{35, 30, 39, 50},
{50, 60, 75, 72}};
int rows = 5;
int search_element = 39;
int ans[] = Saddleback(nums, rows - 1, 0, search_element);
System.out.println("Position of "+search_element+" in the matrix is ("+ans[0] + "," + ans[1]+")");
}
/**
* @param nums, the matrix.
* @param row the current row.
* @param col the current column.
* @param search_element the element that we want to search for.
* @return value: If found the index(row and column) of the element.
* else return (-1 -1).
*/
private static int[] Saddleback(int nums[][], int row, int col, int search_element) {
//numsay to store the row and column of the searched element
int element_pos[] = {-1, -1};
if (row < 0 || col >= nums[row].length) {
return element_pos;
}
if (nums[row][col] == search_element) {
element_pos[0] = row;
element_pos[1] = col;
return element_pos;
}
//move up if the current element is greater than the given element
else if (nums[row][col] > search_element) {
return Saddleback(nums, row - 1, col, search_element);
}
//otherwise move right
return Saddleback(nums, row, col + 1, search_element);
}
/**
* Main method
*
* @param args Command line arguments
*/
}
是的,有一个快速的数学方法。
两个整数在同一列如果num1 % 3 == num2 % 3
两个整数在同一行如果(num1 - 1) / 3 == (num2 - 1) / 3
对于 row-check 重要的是它们是整数,因为整数除法会舍去余数。