You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
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);
|
|
}
|
|
}
|
|
}
|