Blue Bridge cup of java-based practice to find an integer

Find basic training integer

Resource constraints

Time limit: 1.0s Memory Limit: 256.0MB

Problem Description

Comprising a given number of columns n integers, ask a first integer number of columns in the first occurrence of the first few.

Input Format

The first row contains an integer n.

The second line contains a non-negative integer n, for a given number of columns, each column count is not greater than 10,000.

The third row contains an integer a, the number to be searched.

Output Format

If a number appears in the column, it is the position of the first occurrence of the output (position numbering begins at 1), otherwise the output -1.

Sample input

6
1 9 4 8 3 9
9

Sample Output

2

Data size and convention

1 <= n <= 1000。

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        int temp=0;
        Scanner scanner_1=new Scanner(System.in);
        int number_1=scanner_1.nextInt();
        int[] array=new int[number_1];
        for(int i=0;i<number_1;i++){
            int number_2=scanner_1.nextInt();
            array[i]=number_2;
        }       
        int number_3=scanner_1.nextInt();
        for (int i = 0; i < array.length; i++) {
            if(array[i]==number_3){
                temp=i+1;
                break;
            }
        }
        if(temp==0){
            System.out.println(-1);
        }else{
            System.out.println(temp);
        }
        
    }
    
}
Published 24 original articles · won praise 2 · Views 186

Guess you like

Origin blog.csdn.net/weixin_44570019/article/details/104515604