⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 erfen.cpp

📁 实现数据结构中的二分法算法
💻 CPP
字号:
      #include<iostream.h>
      #define N 10
      typedef int keytype ;
      typedef int elemtype ;
      typedef struct
      {    keytype key;                  /* 关键字类型     */
           elemtype other;             /* 其它域  */
      }sqlist;                          /* 顺序表  */
      sqlist R[N+1];
       int binarysearch(sqlist R[],keytype k)
       {
	       int low,mid,high;
	       low=1;
	       high=N;
	       while(low<=high)
	       {
		       mid=(low+high)/2;
		       if(k==R[mid].key)
			       return mid;
		       else
			       if(k<R[mid].key)
				       high=mid-1;
			       else
				       low=mid+1;
	       }
	       return 0;
       }
      void main()
      {
		  int x,i;
          cout<<"Creat the sqlist:";
	      for(i=1;i<=N;i++)
          cin>>R[i].key;
	      while(1)
	      {
		      cout<<"Input the key you want to search:";
              cin>>x;
     	      int a=binarysearch(R,x);
	          if(a!=0)
			      cout<<"It is the " <<a<<"'th  key!"<<endl;
	          else cout<<"It is not in the sqlist!"<<endl;
	      }
       }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -