Problem D: 调用自定义函数search(int list[], int n),在数组中查找某个数

Description

输入10个整数存储到数组a,再输入一个整数x,在数组a中查找x,若找到则输出相应的下标,否则显示"Not found"。要求定义和调用函数search(int list[], int n, int x),在数组list中查找元素x,若找到则返回相应下标,否则返回-1.

Input

多组测试数据,每组先输入10个整数,再输入一个x

Output

输出x在数组中的下标或"Not found"

Sample Input

1 2 3 4 5 6 7 8 9 10 5
1 2 3 4 5 6 7 8 9 10 20

Sample Output

4
Not found

代码实现

#include<iostream>
using namespace std;
int search(int *a,int n)
{
    int i;
    for(i=0;i<=9;i++)
        if(*(a+i)==n)
            return i;
    return -1;
}
int main()
{
    int i,a[10],n;
    while(cin>>a[0])
    {
        for(i=1;i<=9;i++)   cin>>a[i];
        cin>>n;
        if(search(a,n)!=-1) cout<<search(a,n)<<endl;
        else    cout<<"Not found"<<endl;
    }
    return 0;
}
发布了149 篇原创文章 · 获赞 14 · 访问量 9005

猜你喜欢

转载自blog.csdn.net/weixin_45485719/article/details/103375181