LanQiaoTestCodes/查找整数.java

35 lines
983 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}
}