java实现——003二维数组中的查找

时间:2023-03-08 17:21:29
java实现——003二维数组中的查找
 import java.util.Scanner;

 public class T003 {

     public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int rows = 0, cols = 0;
rows = in.nextInt();
cols = in.nextInt();
int a[][] = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = in.nextInt();
}
}
System.out.println(find(a, rows, cols, 7));
} public static boolean find(int a[][], int rows, int cols, int f) {
boolean found = false;
int row = 0;
int col = cols - 1;
while (row < rows && col >= 0) {
if (a[row][col] == f) {
found = true;
break;
} else if (a[row][col] > f) {
--col;
} else {
++row;
}
}
return found;
}
}