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

📄 zhebanchazhao1.cpp

📁 关于数据结构的各章节的c原代码实现
💻 CPP
字号:
// zhebanchazhao1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define  MAX 7
struct element
{
	int key;
};
typedef struct element record;
record data[MAX]={2,5,7,9,17,21,25};
int binarysearch(int key)
{
 int low,high,mid;
 low=0;
 high=MAX-1;
while (low<=high)
{
	 mid=(low+high)/2;
	 if(key<data[mid].key)
		 high=mid-1;
	 else
		 if(key>data[mid].key)
            low=mid+1;
		 else
			 return mid;
}
return -1;
}
int main(int argc, char* argv[])
{
    int find;
	int value;
	while (1)
	{
		printf("\n请输入查找值(0-34)");
		scanf("%d",&value);
		if (value!=-1)
		{
			find=binarysearch(value);
			if(find!=-1)
				printf("找到要查找值:%d[%d]\n",value,find);
			else
                printf("没有找到要查找的值:%d\n",value);
		}
		else
			exit(1);
	}

	return 0;
}

⌨️ 快捷键说明

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