改变以下代码变量名使代码重复率降低的同时保留代码原理与功能,#include <stdio.h> #include <stdlib.h> int BinSearch(int R[], int n, int k) { int low=0,high=n-1,mid,count=0; while(low<=high) { mid=(low+high)/2; printf("第%d次查找:在[ %d ,%d]中找到元素R[%d]:%d\n ",++count,low,high,mid,R[mid]); if(R[mid]==k) return mid; if(R[mid]>k) high=mid-1; else low=mid+1; } return -1; } int main() { int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int k, loc; printf("Please input your number: \n"); scanf("%d", &k); loc=BinSearch(a, 10, k); if(loc == -1) printf("not find!"); return 0; }
时间: 2024-04-26 11:21:38 浏览: 133
SeqList.h #include <stdio.h> //包含标准输入输出流的头文件 #include <assert.h
#include <stdio.h>
#include <stdlib.h>
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1, mid, count = 0;
while(low <= high) {
mid = (low + high) / 2;
printf("The %d time searching: Find element arr[%d]: %d in [%d, %d]\n", ++count, mid, arr[mid], low, high);
if(arr[mid] == key)
return mid;
if(arr[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
int main() {
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int key, index;
printf("Please enter a number: \n");
scanf("%d", &key);
index = binarySearch(arr, 10, key);
if(index == -1)
printf("Not found!\n");
return 0;
}
阅读全文