利用C语言实现这般查找
程序员文章站
2022-05-13 16:08:40
#include
#include
void find(int arr1[], int key, int ri...
#include<stdio.h> #include<stdlib.h> void find(int arr1[], int key, int right) { int left = 0, mid; while (left <= right) { mid = (left + right) / 2; if (key == arr1[mid]) { printf("\n找到了,您要找的数是%d,位置为%d\n", key, mid+1); break; } else if (key > arr1[mid]) left = mid + 1; else right = mid - 1; } if (left = right) printf("\n您要找的数不存在,请重试!"); } int main() { int arr[] = {1, 3 ,4 ,5 ,6, 7, 9 ,13, 15 ,36 }; int k,len; len = sizeof(arr); printf("请输入你要查找的数:"); scanf("%d", &k); find(arr,k,len); system("pause"); return 0; }
上一篇: C++类型转换代码教程
下一篇: 云计算在线编辑器Sed详解