import java.util.*;
public class MakeSpaceTest {
static int white = 0;
static int green = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] spaces = new int[n][n];
for(int r = 0; r < n; ++r) {
for(int c = 0; c < n; ++c) {
spaces[r][c] = sc.nextInt();
}
}
makeSpace(spaces, 0, 0, n);
System.out.println(white);
System.out.println(green);
sc.close();
}
private static void makeSpace(int[][] spaces, int r, int c, int size) {
//영역의 좌상단 위치, 영역 크기
int sum = 0;
for(int i = r; i < r + size; ++i) {
for(int j = c; j < c + size; ++j) {
sum += spaces[i][j];
}
}
//처음 두 조건이 base case가 된다.
//즉 size == 1을 체크할 필요가 없다.
if(sum == 0) { //모든 칸이 0인 경우
++white;
} else if(sum == size*size) { //모든 칸이 1인 경우
++green;
} else {
int halfSize = size/2;
makeSpace(spaces, r, c, halfSize);
makeSpace(spaces, r + halfSize, c, halfSize);
makeSpace(spaces, r, c + halfSize, halfSize);
makeSpace(spaces, r + halfSize, c + halfSize, halfSize);
}
}
}
binarySearch(s[], n, key)
Arrays.binarySearch
import java.util.*;
public class ArraysBinarySearchExample {
public static void main(String[] args) {
int[] a = new int[] {1, 3, 5, 7, 9};
for(int i = 0; i <= 20; i += 2) {
System.out.println(Arrays.binarySearch(a, i));
}
}
}
-1
-2
-3
-4
-5
-6
-6
-6
-6
-6
-6
주어진 배열에 key
가 존재하면 key
의 index가 반환되고, 그렇지 않으면 음수가 반환된다.
이 음수 값은 절대값을 취하면 해당 원소가 삽입되어야 할 위치의 인덱스가 된다.