35 lines
983 B
Java
35 lines
983 B
Java
package LanQiaoOJ;
|
||
/*
|
||
* 问题描述
|
||
给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
|
||
输入格式
|
||
第一行包含一个整数n。
|
||
第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000。
|
||
第三行包含一个整数a,为待查找的数。
|
||
输出格式
|
||
如果a在数列中出现了,输出它第一次出现的位置(位置从1开始编号),否则输出-1。*/
|
||
import java.util.Scanner;
|
||
public class 查找整数{
|
||
static int N=10000;
|
||
static int [] a=new int[N];
|
||
public static void main(String[] args) {
|
||
Scanner sc=new Scanner(System.in);
|
||
int n=sc.nextInt();//确定数组大小
|
||
for(int i=0;i<n;i++) {
|
||
a[i]=sc.nextInt();//将数据存入数组
|
||
}
|
||
int mark=0;
|
||
int s=sc.nextInt();//要查找的数
|
||
for(int i=0;i<a.length;i++) {
|
||
if(a[i]==s) {
|
||
mark=1;
|
||
System.out.println(i+1);
|
||
break;
|
||
}
|
||
}
|
||
if(mark==0) {
|
||
System.out.println(-1);
|
||
}
|
||
}
|
||
}
|