-->
当前位置:首页 > 题库 > 正文内容

程序填空题:折半查找

Luz3年前 (2021-05-10)题库2634
二分查找。
```c++

#include
using namespace std;
#define MAXSIZE 100

typedef struct{
int key;
}ElemType;

typedef struct{
ElemType *R;
int length;
}SSTable;

int Create_SSTable(SSTable &L)
{ int n;
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> L.R[i].key;
L.length++;
}
return 1;
}

int Search_Bin(SSTable ST,int key) {
int low=1,high=ST.length;
int mid;
while(@@[low<=high](2)) {
mid=(low+high) / 2;
if (@@[key==ST.R[mid].key](2)) return mid;
else if (@@[key else low =mid +1;
}
return 0;
}

int main()
{
SSTable ST;
int key;
int result;
ST.R=new ElemType[MAXSIZE];
ST.length=0;
Create_SSTable(ST);
cin >> key;
result=Search_Bin(ST, key);
if(result)
cout << "search success,The key is located in "<< result;
else
cout << "search failed";
return 0;
}
```
### 输入样例:
第一行输入一个数n,第二行输入n个数,第三行输入要查找的数key。
```in
11
5 13 19 21 37 56 64 75 80 88 92
21
```

### 输出样例:

```out
search success,The key is located in 4
```





答案:
第1空:low<=high

第2空:key==ST.R[mid].key

第3空:key

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。