BASIC-5 查找整数

BASIC-5 查找整数

题目

问题描述

给出一个包含 n 个整数的数列,问整数 a 在数列中的第一次出现是第几个。

输入格式

第一行包含一个整数 n。
第二行包含 n 个非负整数,为给定的数列,数列中的每个数都不大于 10000。
第三行包含一个整数 a,为待查找的数。

输出格式

如果 a 在数列中出现了,输出它第一次出现的位置(位置从 1 开始编号),否则输出-1。

样例输入

6
1 9 4 8 3 9
9

样例输出

2

数据规模与约定

1 <= n <= 1000。

题解

import java.util.Scanner;

public class BASIC_5 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int find = sc.nextInt();
        sc.close();
        int re = -1;
        for (int i = 0; i < n; i++) {
            if (arr[i] == find) {
                re = i + 1;
                break;
            }
        }
        System.out.println(re);
    }
}
posted @ 2022-03-18 19:35  morning-start  阅读(26)  评论(0编辑  收藏  举报