November 20, 2017.
1 Importjava.util.Arrays;2 3 Public class Program4 {5 Public Static voidMain (string[] args)6 {7 //testbinarysearch (' + ');8Euclid (104, 24);9 }Ten One Public Static intgcdintPintq) A { -System.out.println ("P:" + P + ", Q:" +q); - if(q = 0)returnp; the intr = p%Q; - returngcd (q, R); - } - + Public Static intLgDoubleN) - { + Doublem = Math.log (n)/Math.log (2.0); A return(int) Math.floor (m); at } - - Public Static int[] Histogram (int[] A,intm) - { - int[] M =New int[m]; - for(inti = 0; i < a.length; i++) { in if(i = =A[i]) -M[i] + = 1; to } + returnM; - } the * Public Static intMysteryintAintb) $ {Panax Notoginseng if(b = = 0)return0; - if(b% 2 = = 0)returnMystery (A + A, B/2); the returnMystery (A + A, B/2) +A; + } A the Public Static LongFibonacciintN) + { - if(n = = 0)return0; $ if(n = = 1)return1; $ Else returnFibonacci (N-2) + Fibonacci (n-1); - } - the Public Static LongFibintN) - {Wuyi LongF1 = 0; the LongF2 = 1; - LongTM = 0; Wu while(N > 0) { -TM =F1; AboutF1 = F1 +F2; $F2 =TM; -n--; - } - returnF1; A } + the Public Static DoublelnintN) - { $ if(n = = 1) the returnMATH.LOG10 (1); the Else the returnMATH.LOG10 (n) + ln (n-1); the } - in Public Static intBinarySearch (intKeyint[] arr,intLointHiintN) the { theSystem.out.println ("deep:" + n++); About if(Lo > Hi)return-1; the intMid = lo + (Hi-lo)/2; the if(Key <Arr[mid]) the returnBinarySearch (Key, arr, lo, mid-1, n); + Else if(Key >Arr[mid]) - returnBinarySearch (Key, arr, Mid + 1, Hi, N); the ElseBayi returnmid; the } the - Public Static intBinarySearch (intKeyint[] arr) - { the returnBinarySearch (key, arr, 0, arr.length, 0); the } the the Public Static voidTestbinarysearch (Charch) - { the int[] arr = {11, 13, 15, 7, 9, 1, 3, 5, 17, 19, 21}; the Arrays.sort (arr); the for(inti = 0; i < arr.length; i++) {94System.out.print (Arr[i] + (i < arr.length-1?) "": "\ n")); the } the intKey = 18; the System.out.println (BinarySearch (key, arr));98 } About - Public Static voidEuclidintPintq)101 {102 intR =gcd (P, q);103 System.out.println (r);104 } the}
Practice Code-Algorithm (fourth edition) (i)