Skip to content

Instantly share code, notes, and snippets.

@mizushou
Created October 9, 2017 18:28
Show Gist options
  • Save mizushou/fb92c7783ef2b7e1a9b7bd7cbb1518ee to your computer and use it in GitHub Desktop.
Save mizushou/fb92c7783ef2b7e1a9b7bd7cbb1518ee to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class LinearSearch {
static boolean search(int[] A, int n, int key) {
int i = 0;
//番兵法
A[n] = key;
while(true) {
if(A[i] == key) {
break;
}
i++;
}
return i == n ? false : true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = new int[10000+1];
for(int i=0; i<n; i++){
A[i] = sc.nextInt();
};
int q = sc.nextInt();
int sum = 0;
long start = System.nanoTime();
for(int i=0; i<q; i++){
int key = sc.nextInt();
if(search(A, n, key)) {
sum++;
}
};
long end = System.nanoTime();
sc.close();
System.out.println("Result : " + sum);
System.out.println("Time : " + (end - start) / 1000000f + "ms");
}
}
@mizushou
Copy link
Author

mizushou commented Oct 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment