利用C语言实现这般查找
程序员文章站
2024-01-20 10:55:52
#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; }
上一篇: mifi好用吗?移动4gmifi怎么用?