(二分查找)输出在数组中第一个大于等于查找值的位置,如果数组中不存在这样的数,则输出数组长度加一。
public static void main(String[] args) { int v = 1; int[] a = {1,2,3,4,5}; int left = 0; int right = a.length - 1; while (left <= right) { int mid = (left + right) / 2; if (a[mid] >= v) { right = mid - 1; } else { left = mid + 1; } } System.out.println(left + 1); }