資源限制
時間限制:1.0s 內存限制:256.0MB
問題描述
給出一個包含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 Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int [1000] ;
for(int i = 0 ; i < n ; i++)
a[i] = sc.nextInt() ;
int key = sc.nextInt() ;
for(int i = 0 ; i < n ; i++) {
if(a[i] == key) {
System.out.println(i+1);
break;
}
if(i == n - 1)
System.out.println(-1);
}
}
}