在数组中查找指定元素。输入一个正整数n(1 < n <= 10),然后输入n个整数存入数组a中,再输入一个整数x,在

2024-11-07 09:03:34
推荐回答(3个)
回答1:

#include
int search(int list[],int n,int x)
{
int i;
for(i=0;i if(list[i]==x)return i;
return -1;
}
main()
{
int list[10],n,x,i;
printf("input n(1<=n<=10):");
scanf("%d",&n);
printf("input %d numbes:\n",n);
for(i=0;i scanf("%d",&list[i]);
printf("input x:");
scanf("%d",&x);
if(search(list,n,x)==-1)printf("N0 Found\n");
else printf("the NO.%d\n",search(list,n,x));
}

回答2:

#include
using namespace std;

int search(int list[],int n,int x)
{
for(int i=0;i {
if(list[i]==x)
{
return i;
break;
}
}
return -1;
}
main()
{
int list[10],n,x,i;
cout<<"Pleaese input n(1<=n<=10):";
cin>>n;
cout<<"\nPleaese input "< for(i=0;i cin>>list[i];
cout<<"\nPleaese input x:";
cin>>x;
if(search(list,n,x)==-1) cout<<"\nN0 Found\n";
else cout<<"\n该数的下标为:"<}

回答3:

#include
int search(int list[],int n,int x)
{
int i;
for(i=0;iif(list[i]==x)return i;
return -1;
}
main()
{
int list[10],n,x,i;
printf("input n(1<=n<=10):");
scanf("%d",&n);
printf("input %d numbes:\n",n);
for(i=0;iscanf("%d",&list[i]);
printf("input x:");
scanf("%d",&x);
if(search(list,n,x)==-1)printf("N0 Found\n");
else printf("the NO.%d\n",search(list,n,x));
}