Problem1073--查找特定的值

1073: 查找特定的值

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 30  Solved: 23
[Submit] [Status] [Web Board] [Creator:]

Description

在一个序列(下标从1开始)中查找一个给定的值,输出第一次出现的位置。

Input

第一行包含一个正整数n,表示序列中元素个数。1 <= n <= 10000。
第二行包含n个整数,依次给出序列的每个元素,相邻两个整数之间用单个空格隔开。元素的绝对值不超过10000。
第三行包含一个整数x,为需要查找的特定值。x的绝对值不超过10000。

Output

若序列中存在x,输出x第一次出现的下标;否则输出-1。

Sample Input

5
2 3 6 7 3
3

Sample Output

2

HINT

#include <iostream>
 using namespace std;
 int main()
 {
   int a[10001] ;
   int i,x,n;
   cin>>n;
   for(i=0;i<n;i++)
    cin>>a[i];
   cin>>x;
  for(i=0;i<n;i++)
  if(a[i]==x) break;
 
  if(i<n)
  printf("%d",i+1);
  else printf("-1");
 }

Source/Category


[Submit] [Status]