NOI sequential search - to find a specific value

description

Find the position of a given value, the output of the first occurrence in a sequence (subscript starting at 1) of.

Entry

The first line contains a positive integer n, the number of elements in the sequence. 1 <= n <= 10000.
The second line contains n integers, each element of the sequence given in sequence, separated by a single space between the adjacent two integers. The absolute value of the element does not exceed 10000.
The third line contains an integer x, is the need to find a specific value. absolute value of x is not more than 10,000. If there is output x, the output of the first occurrence of x index sequence; otherwise outputs -1.

Sample input

5
2 3 6 7 3
3

Sample Output

2

Code

 1 import java.util.*;
 2 
 3 public class Main{
 4     public static void main(String[] args){
 5         Scanner scanner = new Scanner(System.in);
 6         int n = scanner.nextInt();
 7         int[] array = new int[n+1];
 8         for(int i=1;i<=n;++i){
 9             array[i] = scanner.nextInt();
10         }
11         int x = scanner.nextInt();
12         int pos = -1;
13         for(int i=1;i<=n;++i){
14             if(array[i] == x){
15                 pos = i;
16                 break;
17             }
18         }
19         System.out.println(pos);
20     }
21 }

 

Guess you like

Origin www.cnblogs.com/WQG-170603/p/12635936.html