C++ 在数组中查找关键字
答案:2 悬赏:10
解决时间 2021-02-17 15:16
- 提问者网友:迷茫庸人
- 2021-02-17 01:22
定义函数模板 int SeqSearch(T list[], int n,T key); 实现在 在数组 list 的前 n 个元素中查找关键字 key ,若找到,返回对应元素下标,否则返回 -1提示:(1) 主函数中分别定义包含 5 个元素的整型数组 a :int a[5]={1,2,3,4,5}; 和包含 5 个实型元素的数组 b :double b[5]={1.1,2.2,3.3,4.4,5.5};(2) 查找内容从键盘输入,程序中无输入提示语句( 3 )本题输入及输出结果对应如下 :输入内容:3 2.2 < 回车 >输出内容:The index of 3 is:2The index of 2.2 is:1输入内容:6 9.2 < 回车 >输出内容:No found 6No found 9.2
最佳答案
- 二级知识专家网友:佛说妍妍很渣
- 2021-02-17 01:28
#include
using namespace std;
template
int SeqSearch(T list[],int n,T key)
{
int i;
for(i=0; i
{
if(list[i]==key)
{
return i;
}
}
return -1;
}
int main()
{
int x,ix,a[5]={1,2,3,4,5};
double y,iy,b[5]={1.1,2.2,3.3,4.4,5.5};
cin>>x>>y;
ix=SeqSearch(a,5,x);
iy=SeqSearch(b,5,y);
if(ix!=-1)
{
cout<<"The index of "<
}
else
{
cout<<"No found "<
}
if(iy!=-1)
{
cout<<"The index of "<
}
else
{
cout<<"No found "<
}
return 0;
}
using namespace std;
template
int SeqSearch(T list[],int n,T key)
{
int i;
for(i=0; i
if(list[i]==key)
{
return i;
}
}
return -1;
}
int main()
{
int x,ix,a[5]={1,2,3,4,5};
double y,iy,b[5]={1.1,2.2,3.3,4.4,5.5};
cin>>x>>y;
ix=SeqSearch(a,5,x);
iy=SeqSearch(b,5,y);
if(ix!=-1)
{
cout<<"The index of "<
else
{
cout<<"No found "<
if(iy!=-1)
{
cout<<"The index of "<
else
{
cout<<"No found "<
return 0;
}
全部回答
- 1楼网友:我们只是兮以城空
- 2021-02-17 02:56
#include <iostream>
using namespace std;
template<class t>
int seqsearch(t list[],int n,t key)
{
int i;
for(i=0; i<n; i++)
{
if(list[i]==key)
{
return i;
}
}
return -1;
}
int main()
{
int x,ix,a[5]={1,2,3,4,5};
double y,iy,b[5]={1.1,2.2,3.3,4.4,5.5};
cin>>x>>y;
ix=seqsearch(a,5,x);
iy=seqsearch(b,5,y);
if(ix!=-1)
{
cout<<"the index of "<<x<<" is is:"<<ix<<endl;
}
else
{
cout<<"no found "<<x<<endl;
}
if(iy!=-1)
{
cout<<"the index of "<<y<<" is is:"<<iy<<endl;
}
else
{
cout<<"no found "<<y<<endl;
}
return 0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯