4、顺序查找



  1 //
  2 #include<iostream>
  3 //没有排序的数据:只能顺序查找
  4 using namespace std;
  5 /*
  6 a为数组
  7 n为数组大小
  8 x为要查找的数
  9 */
 10 int SquentialSearch(int *a,const int n,const int x);
 11 
 12 int main()
 13 {
 14 	int m[]={2,4,5,8,9,1,6,3,7,0};
 15 	int result;
 16 	int a;
 17 	cout<<"输入要查找的数:"<<endl;
 18 	cin>>a;
 19 	result=SquentialSearch(m,10,a);
 20 	if(result == -1)
 21 		cout<<"没找到"<<endl;
 22 	else
 23 		cout<<"在m["<<result<<"}处找到了"<<m[result]<<endl;
 24 	system("pause");
 25 	return 0;
 26 }
 27 
 28 int SquentialSearch(int *a,const int n,const int x)
 29 {
 30 	int i;
 31 	for(i=0;i<n;i++)
 32 	{
 33 		if(a[i]==x)
 34 		{return i;}
 35 	}
 36 	if(i==n)
 37 		return -1;
 38 }

vs2010运行结果:

image

转载于:https://www.cnblogs.com/luanxin/p/8874825.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值