输入格式
第一行包含一个正整数 n,表示序列中元素个数。1≤n≤100001≤n≤10000。
第二行包含 n 个整数,依次给出序列的每个元素,相邻两个整数之间用单个空格隔开。元素的绝对值不超过 1000010000。
第三行包含一个整数 x,为需要查找的特定值。x 的绝对值不超过 1000010000。
输出格式
若序列中存在 x,输出 x 第一次出现的下标; 否则输出 -1
。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] an= new int[n];
var s= Console.ReadLine();
var num=s.Split(' ');
for (int i = 0; i < n; i++)
{
an[i] = int.Parse(num[i]);
}
int x = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
if (an[i] == x)
{
Console.WriteLine(i);
break;
}
if (i==n-1&&an[i]!= x)
{
Console.WriteLine("-1");
}
}
Console.ReadLine();
}
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_66199253/article/details/137441686