From 5f1daf88f3fa0d140396c48eeb730c896679b929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=A6=E7=A8=8B=E4=BA=91?= Date: Wed, 3 Jun 2020 00:03:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E6=B5=8B=E8=AF=95Code04=5FBSExist.jav?= =?UTF-8?q?a=EF=BC=8C=E8=A1=A5=E5=AF=B9=E6=95=B0=E6=9C=9F=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=E4=BB=A3=E7=A0=81=E6=97=A0=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/class01/Code04_BSExist.java | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/class01/Code04_BSExist.java b/src/class01/Code04_BSExist.java index 9010182..49bc7f2 100644 --- a/src/class01/Code04_BSExist.java +++ b/src/class01/Code04_BSExist.java @@ -1,5 +1,7 @@ package class01; +import java.util.Arrays; + public class Code04_BSExist { public static boolean exist(int[] sortedArr, int num) { @@ -26,5 +28,42 @@ public class Code04_BSExist { } return sortedArr[L] == num; } + + // for test + public static boolean test(int[] sortedArr, int num) { + for(int cur : sortedArr) { + if(cur == num) { + return true; + } + } + return false; + } + + + // for test + public static int[] generateRandomArray(int maxSize, int maxValue) { + int[] arr = new int[(int) ((maxSize + 1) * Math.random())]; + for (int i = 0; i < arr.length; i++) { + arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random()); + } + return arr; + } + + public static void main(String[] args) { + int testTime = 500000; + int maxSize = 10; + int maxValue = 100; + boolean succeed = true; + for (int i = 0; i < testTime; i++) { + int[] arr = generateRandomArray(maxSize, maxValue); + Arrays.sort(arr); + int value = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random()); + if (test(arr, value) != exist(arr, value)) { + succeed = false; + break; + } + } + System.out.println(succeed ? "Nice!" : "Fucking fucked!"); + } }