1723: 查找-二分查找
金币值:2
定数:7
时间限制:1.000 s
内存限制:128 M
正确:65
提交:131
正确率:49.62% 命题人:
题目描述
采用二分查找方式完成指定关键字查找,找到返回对应位置,未找到输出0
测试代码 复制
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int *R;
int length;
} SSTable;
void init(SSTable &ST) {
ST.R=(int *)malloc(MAXSIZE*sizeof(int));
if(!ST.R) exit(0);
ST.length=0;
}
void input(SSTable &ST,int n);
int Search_Bin(SSTable ST,int key);
int main(void) {
SSTable st;
int n,key,index;
init(st);
scanf("%d",&n);
input(st,n);
scanf("%d",&key);
index=Search_Bin(st,key);
printf("%d",index);
return 0;
}
/*以下为你的代码*/
输入格式
第1行:查找表中的元素个数n
第2行:n个元素(保证有序)
第3行:待查找的key
第2行:n个元素(保证有序)
第3行:待查找的key
输出格式
找到返回对应位置,未找到输出0
输入样例 复制
6
1 3 5 7 9 11
5
输出样例 复制
3