package com.harrison.Class01; import java.util.Arrays; public class Code04_BSExist { public static boolean exist(int[] sortedArr,int num) { if(sortedArr==null || sortedArr.length==0) { return false; } int l=0; int r=sortedArr.length-1; int mid=0; while(l<r) { mid=l+((r-l)>>1); if(sortedArr[mid]==num) { return true; }else if(sortedArr[mid]>num) { r=mid-1; }else { l=mid+1; } } return sortedArr[l]==num; } public static boolean test(int[] sortedArr,int num) { for(int cur:sortedArr) { if(cur==num) { return true; } } return false; } public static int[] generateRandomArray(int maxSize,int maxValue) { int[] arr=new int[(int)(Math.random()*(maxSize+1))]; for(int i=0; i<arr.length; i++) { arr[i]=(int)(Math.random()*(maxValue+1))-(int)(Math.random()*maxValue); } return arr; } public static void main(String[] args) { int testTimes=1000000; int maxSize=100; int maxValue=100; boolean succeed=true; for(int i=0; i<testTimes; i++) { int[] arr=generateRandomArray(maxSize, maxValue); Arrays.sort(arr); int num=(int)(Math.random()*(maxValue+1))-(int)(Math.random()*maxValue); if(exist(arr, num)!=test(arr, num)) { succeed=false; break; } } System.out.println(succeed?"Nice":"Fucking fucked"); } }