zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    day10
    day 09
    day08
    day07
    day6
    day5
    成员变量和局部变量
    (第五章)java面向对象之this的作用总结
    简单的音乐播放
    异步消息处理机制 简析
  • 原文地址:https://www.cnblogs.com/luanxin/p/8874825.html
Copyright © 2011-2022 走看看