折半查找法--C语言谭浩强版练习6.9
2015-09-27 12:34
495 查看
不多说,直接上图。
本程序来源于百度百科――折半查找法
本程序来源于百度百科――折半查找法
#include <stdio.h> #include <stdlib.h> int main(void) { int arrayA[15]={2, 4, 5, 7, 8, 10, 12, 13 ,15, 17, 20, 23, 25, 26, 27}; int num; int min=0; int max=14; int mid; int flag=0; printf("Please input the number you want to be find:\n"); scanf("%d", &num); while(min+1!=max) { mid=(min+max)/2; if(arrayA[mid]==num) { printf("The num %d at %d", num, mid); flag=1; break; } else if(arrayA[mid]>num) max=mid; else if(arrayA[mid]<num) min=mid; } if(num==arrayA[max]) printf("输入的数在数列的第%d位\n",max); else if(num==arrayA[min]) printf("输入的数在数列的第%d位\n",min); else if (num!=arrayA[mid]) printf("输入的数不在数列中"); return 0; }
相关文章推荐
- c语言中的易错点!!!
- C/C++语言,对MSB、LSB的读/取处理
- 栈的C语言实现
- SetConsoleTextAttribute()用法
- C语言程序初体验-第十一课-第四题:玩数字
- 在C/C++中调用python的简单笔记part 1
- c++如何返回数组
- 黑马程序员——C语言基础---运算符
- HowTo: Export C++ classes from a DLL
- 孙鑫VC++深入详解:Lesson8 Part3---如何让对话框捕获WM_KEYDOWN消息?
- 检测字符串中有多少个单词改新--C语言谭浩强版例6.8
- C++静态库与动态库
- C++机器学习古典材料
- C语言 gets()和scanf()函数的区别
- C语言格式控制符
- C++ new和delete重载
- 【C++ Primer】高级主题
- 【C++ Primer】面向对象编程与泛型编程
- 【C++ primer】类和数据抽象
- 【C++ Primer】容器和算法