parent
e21b6fb1ed
commit
f2a3f3eff8
@ -0,0 +1,47 @@
|
|||||||
|
import edu.princeton.cs.algs4.In;
|
||||||
|
import edu.princeton.cs.algs4.StdIn;
|
||||||
|
import edu.princeton.cs.algs4.StdOut;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class BianrySearch {
|
||||||
|
|
||||||
|
|
||||||
|
public static int rank(int k,int[] a){
|
||||||
|
int lo=0;
|
||||||
|
int hi=a.length-1;
|
||||||
|
while(lo<=hi){
|
||||||
|
int mid=(lo+hi)/2;
|
||||||
|
if(k>a[mid]){
|
||||||
|
lo=mid+1;
|
||||||
|
}
|
||||||
|
else if(k<a[mid]){
|
||||||
|
hi=mid-1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String args[]){
|
||||||
|
|
||||||
|
FileInputStream ins;
|
||||||
|
//输入重定向
|
||||||
|
try{
|
||||||
|
ins = new FileInputStream("F:\\所有学习的课程资料\\算法\\课本例题代码数据/tinyT.txt");
|
||||||
|
System.setIn(ins);
|
||||||
|
}catch(Exception e){
|
||||||
|
System.out.println(e.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] whitelist=In.readInts(args[0]);
|
||||||
|
Arrays.sort(whitelist);
|
||||||
|
while(!StdIn.isEmpty()){
|
||||||
|
int key=StdIn.readInt();
|
||||||
|
if(rank(key,whitelist)<0)
|
||||||
|
StdOut.println(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue