我们得到了二维数组,arr[n][n]。我们选择任意索引,任务是计算周围元素的最小值和最大值(如果在角落,则至少为 3,如果在中间某个位置,则至少为 8)。不要要求你们帮我解决这个问题,而是就如何更好地执行提出建议。
最佳答案
给定数组中的位置 (x, y),您需要访问周围的每个条目。
for ( int i = -1; i <= 1; i++ ) {
for ( int j = -1; j <= 1; j++ ) {
// Don't visit off-array locations.
if ( inArray(x+i,y+j) ) {
// Don't visit the center cell (you wanted the 8 surrounding cells).
if ( i != 0 && j != 0 ) {
check (x+i,y+j);
}
}
}
}
关于java - 我们得到了二维数组,arr (n)(n)。 e 选择任意索引,任务是计算周围元素的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19431539/