Created
October 9, 2017 18:28
-
-
Save mizushou/fb92c7783ef2b7e1a9b7bd7cbb1518ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
線形探索(番兵法)