This problem required to achieve a simple lookup function specified elements in the array.
Function interface definition:
int search( int list[], int n, int x );
List where [] is an array of incoming user; n (≥0) is List [] number of elements; x is element be searched. If you find
Search function returns the minimum index of the corresponding element (index starts from 0), -1 otherwise.
Referee test program Example:
#include <stdio.h>
#define MAXN 10
int search( int list[], int n, int x );
int main()
{
int i, index, n, x;
int a[MAXN];
scanf("%d", &n);
for( i = 0; i < n; i++ )
scanf("%d", &a[i]);
scanf("%d", &x);
index = search( a, n, x );
if( index != -1 )
printf("index = %d\n", index);
else
printf("Not found\n");
return 0;
}
/* 你的代码将被嵌在这里 */
Sample Input 1:
5
1 2 2 5 4
2
Output Sample 1:
index = 1
Sample Input 2:
5
1 2 2 5 4
0
Output Sample 2:
Not found
int search( int list[], int n, int x )
{
int j;
for(j=0;j<n;j++)
{
if(x==list[j])
return j;
}
return -1;
}