寻找整数

问题描述

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

输入格式

第一行包含一个整数n。

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

第三行包含一个整数a,为待查找的数。

输出格式

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

样例输入

6
1 9 4 8 3 9
9



package com.hello.ds;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main 
{

static int fun(int[] num,int nn)
{
for(int i=0;i<num.length;i++)
{
if(num[i]==nn)
return i+1;
}
return -1;
}

public static void main(String[] args) throws NumberFormatException, IOException 
{
BufferedReader bf1=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(bf1.readLine());
String[] s1=bf1.readLine().split(" ");
int[] num=new int[n];
for(int i=0;i<n;i++)
num[i]=Integer.parseInt(s1[i]);
int nn=Integer.parseInt(bf1.readLine());
System.out.println(fun(num, nn));

}

}





猜你喜欢

转载自blog.csdn.net/purpose1123/article/details/79771228