思路:采用数组法,在遍历前4个方块的时候,将他们周围的元素4个方块置位-1;
public static int number(int m,int n) { int[][] array = new int[m][n];//数组初始值为0 int count = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (array[i][j] == 0) { count++; if (i + 2 < m) { array[i + 2][j] = -1; } if (j + 2 < n) { array[i][j + 2] = -1; } } } } return count; } public static int number(int m,int n){ int count=0; if(m%4==0||n%4==0){ count=m*n/2; }else{ count=(m*n/2)+1; } return count; }